Ios 滚动视图中的快捷键点击手势和长按手势,点击指示不起作用
我正在努力在滚动视图中同时实现Tap手势和LongPress手势。使用.ontapsignature和.onlongpresssignature时一切都正常,但我希望当用户点击按钮时,它的不透明度会降低,就像普通按钮一样() 但是,无论出于何种原因,Button()都不能在长按时执行某些操作。所以我试着使用.signature(longpresssignature()…)。此方法有效,并显示抽头指示。不幸的是,这不适用于ScrollView:您不能再滚动它了 所以我做了一些研究,发现在长按手势之前必须有一个点击手势,这样ScrollView才能正常工作。确实如此,但我的长按手势不再起作用了 希望有人能找到解决办法Ios 滚动视图中的快捷键点击手势和长按手势,点击指示不起作用,ios,swift,xcode,swiftui,Ios,Swift,Xcode,Swiftui,我正在努力在滚动视图中同时实现Tap手势和LongPress手势。使用.ontapsignature和.onlongpresssignature时一切都正常,但我希望当用户点击按钮时,它的不透明度会降低,就像普通按钮一样() 但是,无论出于何种原因,Button()都不能在长按时执行某些操作。所以我试着使用.signature(longpresssignature()…)。此方法有效,并显示抽头指示。不幸的是,这不适用于ScrollView:您不能再滚动它了 所以我做了一些研究,发现在长按手势之
struct ContentView: View {
var body: some View {
ScrollView(.horizontal){
HStack{
ForEach(0..<5){ _ in
Button()
}
}
}
}
}
struct Button: View{
@GestureState var isDetectingLongPress = false
@State var completedLongPress = false
var body: some View{
Circle()
.foregroundColor(.red)
.frame(width: 100, height: 100)
.opacity(self.isDetectingLongPress ? 0 : 1)
// That works, but there is no indication for the user that the UI recognized the gesture
// .onTapGesture {
// print("Tapped!")
// }
// .onLongPressGesture(minimumDuration: 0.5){
// print("Long pressed!")
// }
// The approach (*) shows the press indication, but the ScrollView is stuck because there is no TapGesture
// If I add a dummy TapGesture, the LongPressGesture won't work anymore but now the ScrollView works as expected
//.onTapGesture {}
// (*)
.gesture(LongPressGesture()
.updating(self.$isDetectingLongPress) { currentstate, gestureState,
transaction in
gestureState = currentstate
}
.onEnded { finished in
self.completedLongPress = finished
}
)
}
}
结构ContentView:View{
var body:一些观点{
滚动视图(.horizontal){
HStack{
ForEach(0..我已经尝试了多种组合,尝试了OnTap手势+LongPress手势+自定义计时和动画以及许多工作/几乎/但留下了一些小麻烦。这是我发现的效果非常好的组合。在iOS 13.6上测试
使用此解决方案,您的滚动视图仍会滚动,您会看到按钮按下动画,长时间按下按钮也会起作用
struct MainView: View {
...
Scrollview {
RowView().highPriorityGesture(TapGesture()
.onEnded { _ in
// The function you would expect to call in a button tap here.
})
}
}
struct RowView: View {
@State var longPress = false
var body: some View {
Button(action: {
if (self.longPress) {
self.longPress.toggle()
} else {
// Normal button code here
}
}) {
// Buttons LaF here
}
// RowView code here.
.simultaneousGesture(LongPressGesture(minimumDuration: 0.5)
.onEnded { _ in
self.longPress = true
})
}}
谢谢!很好的解决方案!对我也有用:)如果该项目位于滚动视图中,并且您希望该项目也具有长距离跳转功能……这似乎是苹果无法理解的,因为它仍然很出色!?@Learn2Code如果您交换.simultaneosportage和.highprioritysportage的位置,那么它将与两个相同的手势一起工作