SwiftUI:多点触摸手势/多个手势

问题描述 投票:1回答:1

SwiftUI中是否可以同时跟踪多个手势?我希望我的一个主视图能够跟踪多个手指一次拖动。

ZStack {
    Color.black
      .edgesIgnoringSafeArea(.all)
      .gesture(DragGesture(minimumDistance: 0)
               .onChanged { (value) in
                 //some logic
               }.onEnded { (value) in
                  //more logic         
               })
       //other code
}

我有此代码,但是一次只能处理一个拖动手势。如果一根手指在拖动,然后尝试添加另一根,则第一个停止。

我正在尝试实现多个手指同时显示在屏幕上的效果。每个手指同时拖动一个圆圈(每个手指跟随一个圆圈)。

我在Apple文档中看到simultaneous gestures,但这是指使一个手势触发多个块。

swiftui gesture multi-touch
1个回答
0
投票

使用UIViewRepresentable的解决方案(How to detect a tap gesture location in SwiftUI?)将起作用。尽管它不是纯粹的swiftUI解决方案(我一直在寻找一种纯粹的swiftUI解决方案,但找不到一个解决方案,并且希望看到一个)。我已经将代码更新为以下代码,您需要更改numberOfTouchPoints = 5,然后使用回叫将值显示在视图中。

struct TapView: UIViewRepresentable {
    var tappedCallback: (() -> Void)

    func makeUIView(context: UIViewRepresentableContext<TapView>) -> TapView.UIViewType {
        let v = UIView(frame: .zero)
        let gesture = NFingerGestureRecognizer(target: context.coordinator,
                                                       action: #selector(Coordinator.tapped))
        v.addGestureRecognizer(gesture)
        return v
    }

    class Coordinator: NSObject {
        var tappedCallback: (() -> Void)

        init(tappedCallback: @escaping (() -> Void)) {
            self.tappedCallback = tappedCallback
        }

        @objc func tapped(gesture:NFingerGestureRecognizer) {
            for i in 0..<gesture.numberOfTouches{
                print(gesture.location(ofTouch: i, in: gesture.view))
            }
            self.tappedCallback()
        }
    }

    func makeCoordinator() -> TapView.Coordinator {
        return Coordinator(tappedCallback:self.tappedCallback)
    }

    func updateUIView(_ uiView: UIView,
                      context: UIViewRepresentableContext<TapView>) {
    }
}

class NFingerGestureRecognizer: UIGestureRecognizer {
    var numberOfTouchPoints: Int = 2
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        if self.state == .possible {
            if (self.numberOfTouches == numberOfTouchPoints){ // we have a two finger interaction starting
                self.state = .began
                print("two finger drag - Start")
            }
        } else {        // check to see if there are more touchs
            if (self.numberOfTouches > numberOfTouchPoints){ // we have a two finger interaction starting
                self.state = .failed
                print("two finger drag - failed")
            }
        }
        //printInternalTouches()
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        if (self.state != .possible){
            self.state = .changed        // we are looking for two finger interaction
            //printInternalTouches()
            print("changed")
        }
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        self.state = .failed
        //printInternalTouches()
        print("ended")
    }

    private func printInternalTouches(){
        // now print the internal touchUI elements - should be the better solution
        print("Internal Locations")
        for i in 0..<self.numberOfTouches{
            print(self.location(ofTouch: i, in: self.view))
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.