我已经在视图中创建了一个DragGesture,无论用户向左或向右滑动,该视图都应选择@State(Bool)。
事实是只检测到向右滑动。
如何使用.gesture()捕获用户是在屏幕上向左还是向右滑动?
import SwiftUI
struct SwiftUIView: View {
//MARK: Value to change on swipe gesture
@State var swipeRight: Bool
var body: some View {
VStack {
//MARK: Value displayed after user swiped
Text($swipeRight ? "Right" : "Left")
}
.gesture(
DragGesture()
.onChanged { (value) in
//MARK: What is it missing here?
switch value.location.x {
case ...(-0.5):
self.swipeRight = false
print("Swipe Left return false")
case 0.5...:
self.swipeRight = true
print("Swipe Right return true")
default: ()
}
})
}
我已经在视图中创建了一个DragGesture,无论用户向左或向右滑动,该视图都应选择@State(Bool)。事实是,仅检测到向右滑动。如何使用.gesture()捕获...
您应该比较旧位置和新位置: