我有一个关于 Swift 中 UIPanGestureRecognizer 的奇怪案例。 我有一个处理平移手势的函数,并在 UIGestureRecognizerDelegate 中指定“false”,这样就没有其他手势会干扰平移。
这是有问题的案例:
- 用户用一根手指触摸并开始平移
- 用户将第二根手指放在屏幕上 - 第二根手指被忽略
- 用户抬起第一根手指,而第二根手指仍在触摸 - 此时我的处理程序被调用
recognizer.state == .ended
。问题是此时的位置(我通过调用recognizer.location(in: recognizer.view)
获得)返回点(0,0)
<
我是否使用了错误的方式来理解要点?似乎由于第二根手指被完全忽略,第一根手指应该像普通的平移一样,我会在状态==结束时获取位置。
最佳答案
我刚刚测试过 - 当识别器结束时位置不再可用(使用以下)
recognizer.location(ofTouch: 0, in: recognizer.view)
我会添加一个变量来跟踪 .began 和 .changed 中的最后一个已知点(我从中获取数据)然后在 .ended 中使用该数据
编辑:通过子类化获取数据
class pgr: UIPanGestureRecognizer {
var myTouch:Set<UITouch>!
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
myTouch = touches
super.touchesEnded(touches, with: event)
}
}
然后回到你的实现
if sender.state == .ended {
print(sender.myTouch.first?.location(in: sender.view))
}
关于ios - recognizer.location(在 : recognizer. View 中) swift 返回零点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441545/