【问题标题】:How to run code when swiftui gesture recognizers beginswiftui手势识别器开始时如何运行代码
【发布时间】:2020-01-20 04:22:59
【问题描述】:

SwiftUI MagnificationGestureDragGesture 具有 .onChanged.onEnded API,但没有像在 UIKit 中那样检查手势何时开始。我想到的几种方法:

  • .$gestureStarted onChanged 中的 bool,然后在 .onEnded 中将其设置回 false
  • 使用轻击手势序列。

我错过了一些首选的方法吗?想要检查似乎是一件很自然的事情。

【问题讨论】:

    标签: swiftui gesture-recognition


    【解决方案1】:

    有专门的@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。看起来对我正在解决的几个问题很有帮助。感谢您的详细回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多