假设我有两个手指触摸iPhone的屏幕,但只有一个手指在移动。
TouchesMoved事件只会显示一个手指。
我怎么知道TouchesMoved是指哪一个手指?
TouchesMoved事件只会显示一个手指。
我怎么知道TouchesMoved是指哪一个手指?
UIView
上启用多点触控:self.multipleTouchEnabled = true
然后为UITouch
对象保留一个字典。相同的UITouch
对象会在touchesBegan
,touchesMoved
和touchesEnded
中传递:
var touchTypes = Dictionary<UITouch, Int>()
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touchObject in touches {
touchTypes.updateValue(i, forKey: touch as UITouch) //determine i for your own implementation
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
let type = touchTypes[touch] //depending on this value, do something
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touchObject in touches {
touchTypes.removeValueForKey(touchObject as UITouch)
}
}
我发现我想要的是可能实现的。只需在iTunesU上检查斯坦福大学的CS193P课程。
不幸的是,如果你考虑一下,就没有一种“确定性”的方法将一个手指与一个触摸点关联起来。毕竟,你的手指并没有全球唯一的ID,iPhone也无法对其进行采样。
你需要做的是记录“先前”的位置,这对于管理捏合等操作非常有用 - 并根据接近先前触摸设置的距离标记每个手指。
在TouchesBegan
方法中,您将获得一个NSSet *
作为触摸点,您需要循环遍历所有触摸点,并将它们放入您的应用程序上下文中,以便稍后可以识别它们。
如果您描述了您的意图,那么帮助您会更容易...
touches
集合总是只包含单个对象。 - kelin你写道
TouchesMoved 只会显示一个手指 (事件)。
但实际上这并不是正确的:只要有两个手指触摸屏幕,并且其中至少一个手指移动,你就会得到一个 touchesMoved
事件,包含两个手指。
如果其中一个手指暂时被抬起,你可能会收到一些只有一个手指的 touchesMoved
调用,因此你需要决定如何处理这种情况。