嘿,我感动了一下代码,@Sam_M给了我。
我试图基本上跟踪多个手指在移动时的位置。就像手指1在这里,手指2在这里。因此,截至目前,它将打印视图上当前的手指/触摸数和位置,但不会分别跟踪两个手指。Ive环顾了stackoverflow上仅有的其他3个问题。但是没有人给我一个很好的结果,我可以迅速实现。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { for touch: UITouch in event!.allTouches()! { for (index,touch) in touches.enumerate() { let ptTouch = touch.locationInNode(self.view) print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)") } }
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { for touch: UITouch in event!.allTouches()! { for (index,touch) in touches.enumerate() { let ptTouch = touch.locationInNode(self.view) print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)") } } }
每个手指的触摸对象在屏幕上时,具有相同的内存地址将保持不变。您可以通过在阵列中存储触摸对象的地址,然后与该阵列进行比较以准确知道哪个手指在移动,来跟踪多点触摸场景中的各个手指。
var fingers = [String?](count:5, repeatedValue: nil) override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesBegan(touches, withEvent: event) for touch in touches{ let point = touch.locationInView(self.view) for (index,finger) in fingers.enumerate() { if finger == nil { fingers[index] = String(format: "%p", touch) print("finger \(index+1): x=\(point.x) , y=\(point.y)") break } } } } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesMoved(touches, withEvent: event) for touch in touches { let point = touch.locationInView(self.view) for (index,finger) in fingers.enumerate() { if let finger = finger where finger == String(format: "%p", touch) { print("finger \(index+1): x=\(point.x) , y=\(point.y)") break } } } } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesEnded(touches, withEvent: event) for touch in touches { for (index,finger) in fingers.enumerate() { if let finger = finger where finger == String(format: "%p", touch) { fingers[index] = nil break } } } } override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { super.touchesCancelled(touches, withEvent: event) guard let touches = touches else { return } touchesEnded(touches, withEvent: event) }
var fingers = [UITouch?](repeating: nil, count:5) override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) for touch in touches{ let point = touch.location(in: self.view) for (index,finger) in fingers.enumerated() { if finger == nil { fingers[index] = touch print("finger \(index+1): x=\(point.x) , y=\(point.y)") break } } } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesMoved(touches, with: event) for touch in touches { let point = touch.location(in: self.view) for (index,finger) in fingers.enumerated() { if let finger = finger, finger == touch { print("finger \(index+1): x=\(point.x) , y=\(point.y)") break } } } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesEnded(touches, with: event) for touch in touches { for (index,finger) in fingers.enumerated() { if let finger = finger, finger == touch { fingers[index] = nil break } } } } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesCancelled(touches, with: event) guard let touches = touches else { return } touchesEnded(touches, with: event) }
*根据苹果公司更新的文档,现在可以在多点触摸序列中保留触摸,只要它们在序列结束时释放即可