如何在swiftui手势识别器启动时运行代码
SwiftUI如何在swiftui手势识别器启动时运行代码,swiftui,gesture-recognition,Swiftui,Gesture Recognition,SwiftUI手势和Dragesture具有.onChanged和.onededAPI,但与UIKit中一样,手势启动时无需检查。我想到的几种方法: $gestureStarted在onChanged中bool,然后在中将其设置回false 使用带有轻触的手势序列 我是否错过了一些更好的方法?想要检查似乎是很自然的事情 有专门的@GestureState,可用于此目的。所以,这里有一种可能的方法 struct TestGestureBegin: View { enum Progre
手势
和Dragesture
具有.onChanged
和.oneded
API,但与UIKit中一样,手势启动时无需检查。我想到的几种方法:
在onChanged中bool,然后在$gestureStarted
中将其设置回false
- 使用带有轻触的手势序列
我是否错过了一些更好的方法?想要检查似乎是很自然的事情 有专门的
@GestureState
,可用于此目的。所以,这里有一种可能的方法
struct TestGestureBegin: View {
enum Progress {
case inactive
case started
case changed
}
@GestureState private var gestureState: Progress = .inactive // initial & reset value
var body: some View {
VStack {
Text("Drag over me!")
}
.frame(width: 200, height: 200)
.background(Color.yellow)
.gesture(DragGesture(minimumDistance: 0)
.updating($gestureState, body: { (value, state, transaction) in
switch state {
case .inactive:
state = .started
print("> started")
case .started:
state = .changed
print(">> just changed")
case .changed:
print(">>> changing")
}
})
.onEnded { value in
print("x ended")
}
)
}
}
很好,我没有遇到
@gestureState
。看起来对我正在解决的几个问题很有帮助。谢谢你的详细回复。