首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI:多点触控手势/多个手势

SwiftUI:多点触控手势/多个手势
EN

Stack Overflow用户
提问于 2020-05-03 06:05:44
回答 2查看 2.8K关注 0票数 11

在SwiftUI中有没有一种方法可以一次跟踪多个手势?我希望我的一个主视图能够同时跟踪多个手指的拖动。

代码语言:javascript
复制
ZStack {
    Color.black
      .edgesIgnoringSafeArea(.all)
      .gesture(DragGesture(minimumDistance: 0)
               .onChanged { (value) in
                 //some logic
               }.onEnded { (value) in
                  //more logic         
               })
       //other code
}

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

我正在尝试实现多个手指同时出现在屏幕上的效果。每个手指同时拖动一个圆(每个手指后面跟着一个圆)。

我在苹果的文档中看到了simultaneous gestures,但这指的是让一个手势触发多个块。

EN

回答 2

Stack Overflow用户

发布于 2020-05-21 09:04:10

使用UIViewRepresentable的解决方案(How to detect a tap gesture location in SwiftUI?)可以工作。尽管它不是纯粹的swiftUI解决方案(我一直在寻找纯粹的swiftUI解决方案,但是找不到一个,并且很有兴趣看到一个)。我已经将代码更新为下面的代码,您需要更改numberOfTouchPoints = 5。然后使用回调将值放入视图中。

代码语言:javascript
复制
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))
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2021-02-13 00:57:21

我将Paul的答案与苹果的过时示例相结合,创建了一种检测任意数量触摸的方法。

代码语言:javascript
复制
import Foundation
import UIKit

class NFingerGestureRecognizer: UIGestureRecognizer {

    var tappedCallback: (UITouch, CGPoint?) -> Void

    var touchViews = [UITouch:CGPoint]()

    init(target: Any?, tappedCallback: @escaping (UITouch, CGPoint?) -> ()) {
        self.tappedCallback = tappedCallback
        super.init(target: target, action: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        for touch in touches {
            let location = touch.location(in: touch.view)
            print("Start: (\(location.x)/\(location.y))")
            touchViews[touch] = location
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        for touch in touches {
            let oldLocation = touchViews[touch]!
            let newLocation = touch.location(in: touch.view)
            touchViews[touch] = newLocation
            print("Move: (\(oldLocation.x)/\(oldLocation.y)) -> (\(newLocation.x)/\(newLocation.y))")
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        for touch in touches {
            let oldLocation = touchViews[touch]!
            touchViews.removeValue(forKey: touch)
            print("End: (\(oldLocation.x)/\(oldLocation.y))")
        }
    }

}

更新:

您需要将其包装到UIViewRepresentable中以将其用作SwiftUI组件:

代码语言:javascript
复制
import Foundation
import SwiftUI

struct TapView: UIViewRepresentable {

    var tappedCallback: (UITouch, CGPoint?) -> Void

    func makeUIView(context: UIViewRepresentableContext<TapView>) -> TapView.UIViewType {
        let v = UIView(frame: .zero)
        let gesture = NFingerGestureRecognizer(target: context.coordinator, tappedCallback: tappedCallback)
        v.addGestureRecognizer(gesture)
        return v
    }
    
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<TapView>) {
        // empty
    }

}

更新2:

下面是一个如何在SwiftUI中使用TapView的小示例:

代码语言:javascript
复制
var body: some View {
    guard let img = UIImage(named: "background.png") else {
        fatalError("Unable to load image")
    }
    return GeometryReader { geometry in
        ZStack {
            Image(uiImage: img)
                    .resizable()
                    .aspectRatio(geometry.size, contentMode: .fill)
                    .edgesIgnoringSafeArea(.all)


            TapView { touch, optLocation in
                // touch can be used as a dictionary key.
                // optLocation is nil when the touch ends.
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61566929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档