How to track multiple touches

Hey, I have a good code code in my touch, it was passed to me by @Sam_M.

Im trying to basically track the location of a few fingers while moving. Like Finger 1, here is Finger 2. Thus, now it will print the number of fingers / touches that are currently in the image and in place, but it will not track individual fingers separately. Ive looked around for 3 other questions that are on stackoverflow. But no one gave me a good result, which I can implement in quick mode.

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)") } } } 
-1
source share
2 answers

The touch object for each finger will remain unchanged with the same memory address as on the screen. You can track individual fingers in a multi-touch scenario by storing the address of the sensor objects in an array and then comparing it with this array to know exactly which finger is moving.

 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) } 

Updated for quick 4

Credit @Klowne

 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) } 

* According to updated updated documentation, Apple is now OK to save strokes during multiple touch sequences until they are released at the end of the sequence

+2
source

Touch is a finger. A finger is a touch. You can identify a finger by identifying a touch; the touch for a specific finger will be identical with the same object as long as that finger touches the screen. To get a unique identifier for a specific UITouch, take its memory address, for example:

 let uuid = String(format: "%p", theTouch) 

You can save a unique identifier for each touch in the property. But do not keep the touch itself; Apple forbids this.

+1
source

Source: https://habr.com/ru/post/982942/


All Articles