SwiftUIMagnificationGesture
并DragGesture
具有.onChanged
和.onEnded
API,但是无需像UIKit中一样检查手势何时开始。我认为可以这样做的几种方式:
.$gestureStarted
在onChanged中进行bool,然后在inChange中将其设置回false .onEnded
我是否缺少一些首选的方法?要检查似乎是很自然的事情。
有特殊的@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")
}
)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句