SwiftUI MagnificationGesture
和DragGesture
都有.onChanged
和.onEnded
API,但是无需检查手势何时开始,就像在UIKit中一样。我想有几种方法可以做到:
.$gestureStarted
bool在onChanged中,然后在.onEnded
的手势序列。
我是不是错过了做这件事的首选方法?想检查一下似乎是很自然的事。
发布于 2020-01-20 06:11:07
有特殊的@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")
}
)
}
}
https://stackoverflow.com/questions/59816958
复制相似问题