当所需的触摸次数超过一次时,如何获得 UILongPressGestureRecognizer 的触摸位置?

Posted

技术标签:

【中文标题】当所需的触摸次数超过一次时,如何获得 UILongPressGestureRecognizer 的触摸位置?【英文标题】:How do you get the location of touches for a UILongPressGestureRecognizer when the number of required touches is more than one in swift? 【发布时间】:2020-11-21 21:06:42 【问题描述】:

我想从 UILongPressGuestureRecognizer 中获取多个多次触摸的位置,其中所需的触摸次数不止一个。我知道我可以执行以下操作来获取使用一根手指时的位置。

let twoFingerLongPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(twoFingerLongPressChordTouchView(gesture:)))
twoFingerLongPressGesture.numberOfTouchesRequired = 2
twoFingerLongPressGesture.minimumPressDuration = 0.02
self.addGestureRecognizer(twoFingerLongPressGesture)
@objc func twoFingerLongPressAction(gesture: UILongPressGestureRecognizer) 
    let location = gesture.location(in: self)

我也知道我可以在触摸开始回调中迭代触摸,但是我正在尝试利用 UILongPressGestureRecognizer 中的特定行为。提前感谢您的任何帮助或建议!!!

【问题讨论】:

【参考方案1】:

您可以将location(ofTouch:in:)numberOfTouches 结合使用:

let touchPoints = (0..<twoFingerLongPressGesture.numberOfTouches).map 
    twoFingerLongPressGesture.location(ofTouch: $0, in: self)

这将为您提供CGPoints 数组中的所有接触点。

【讨论】:

以上是关于当所需的触摸次数超过一次时,如何获得 UILongPressGestureRecognizer 的触摸位置?的主要内容,如果未能解决你的问题,请参考以下文章