Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swiftui手势识别器启动时运行代码_Swiftui_Gesture Recognition - Fatal编程技术网

如何在swiftui手势识别器启动时运行代码

如何在swiftui手势识别器启动时运行代码,swiftui,gesture-recognition,Swiftui,Gesture Recognition,SwiftUI手势和Dragesture具有.onChanged和.onededAPI,但与UIKit中一样,手势启动时无需检查。我想到的几种方法: $gestureStarted在onChanged中bool,然后在中将其设置回false 使用带有轻触的手势序列 我是否错过了一些更好的方法?想要检查似乎是很自然的事情 有专门的@GestureState,可用于此目的。所以,这里有一种可能的方法 struct TestGestureBegin: View { enum Progre

SwiftUI
手势
Dragesture
具有
.onChanged
.oneded
API,但与UIKit中一样,手势启动时无需检查。我想到的几种方法:

  • $gestureStarted
    在onChanged中bool,然后在
    中将其设置回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
。看起来对我正在解决的几个问题很有帮助。谢谢你的详细回复。