[英]How to select an item by a button or tapGesture in my SwiftUI List and transfer it to another view
[英]TapGesture and Button is not working in list if adding simultaneousGesture with minimumDistance: 0.0 - SwiftUI
我需要使用它提供的可重用性列表,并基于添加simultaneousGesture
和DragGesture(minimumDistance: 0.0)
来支持 iOS 15 来禁用滚动scrollDisabled
仅适用于 iOS 16。
因此,当将手势添加到列表时, onTapGesture
和Button
在列表的每一行内都不起作用。
这是一个示例代码:
ZStack {
List {
ForEach(0..<10, id:\.self) { index in
VStack {
Text("Sample text \(index)")
.onTapGesture {
print("Sample Text tapped \(index)")
}
Button("Sample button \(index)") {
print("Sample button tapped \(index)")
}
.buttonStyle(.bordered)
.frame(height: 50)
}
.frame(height: 100)
}
}
.simultaneousGesture(
DragGesture(minimumDistance: 0.0)
.onChanged({ _ in
print("Drag onChanged")
})
.onEnded({ _ in
print("Drag onEnded")
})
)
}
如果我们将minimumDistance
设置为0.1
,tapGesture 和按钮将起作用,但不再禁用滚动。
你有什么想法或解决方法吗? 谢谢。
我不确定我是否正确理解了你的问题。 我想你想要做的是保持 onTapGesture、Button 和 List 的滚动功能全部正常工作,除非发生拖动事件。
如果您将 simultaneousGesture 放在 ForEach 中而不是将其分配给列表,那么它们都可以工作,除非您拖过 Text 或 Button 元素。 在这种情况下,卷轴不起作用。
struct ContentView: View {
var body: some View {
ZStack {
List {
ForEach(0..<10, id:\.self) { index in
VStack {
Text("Sample text \(index)")
.onTapGesture {
print("Sample Text tapped \(index)")
}
Button("Sample button \(index)") {
print("Sample button tapped \(index)")
}
.buttonStyle(.bordered)
.frame(height: 50)
}
.frame(height: 100)
}
.simultaneousGesture(
DragGesture(minimumDistance: 0.0)
.onChanged { _ in
print("Drag onChanged")
}
.onEnded { _ in
print("Drag onEnded")
}
)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.