Get multiple touch coordinates in Swift

So, I was busy trying to get the coordinates of the touches on the screen. So far I can get the coordinates of one touch with this:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let touch = touches.anyObject()! as UITouch let location = touch.locationInView(self.view) println(location) } 

But when touching with two fingers, I get the coordinates of the first touch. Multi-touch works (I tested this short tutorial: http://www.techotopia.com/index.php/An_Example_Swift_iOS_8_Touch,_Multitouch_and_Tap_Application ). Therefore, my question is: how can I get the coordinates of the second (and third, fourth ...) touch?

+6
source share
6 answers

** Updated to Swift 4 and Xcode 9 (October 8, 2017) **

First of all, be sure to enable multi-touch events by setting

 self.view.isMultipleTouchEnabled = true 

in your UIViewController code UIViewController or using the appropriate storyboard option in Xcode:

xcode screenshot

Otherwise, you always get one touch in touchesBegan ( see documentation here ).

Then, inside touchesBegan sort through a lot of touches to get their coordinates:

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { let location = touch.location(in: self.view) print(location) } } 
+14
source

this argument touches is a set of detected touches. You see only one touch, because you select one of the touches:

 touches.anyObject() // Selects a random object (touch) from the set 

To get all touches, iterate over the given set

 for obj in touches.allObjects { let touch = obj as UITouch let location = touch.locationInView(self.view) println(location) } 
+4
source

You need to iterate over different touches. This way you can access every touch.

 for touch in touches{ //Handle touch let touchLocation = touch.locationInView(self.view) } 
+1
source

In Swift 1.2, this has changed, and touchesBegan now provides a set of NSObjects. To pass through them, draw a collection of touches as a collection of UITouch objects as follows:

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { var touchSet = touches as! Set<UITouch> for touch in touchSet{ let location = touch.locationInView(self.view) println(location) } } 
+1
source

For Swift 3, based on @Andrew's answer:

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { let touchSet = touches for touch in touchSet{ let location = touch.location(in: self.view) print(location) } } 

EDIT. I had a bad one that did not answer your question, had the same problem, and someone connected me with this previous answer :

In any case, I had to change a few things to make it work in swift 3, here is my current code:

 var fingers = [String?](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] = String(format: "%p", 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 == String(format: "%p", 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 == String(format: "%p", touch) { fingers[index] = nil break } } } } 

I still have a little problem, but I think this is due to my GestureRecognizer in my code. But that should do the trick. It will print you the coordinates of each point in the console.

0
source

In Swift 3.4, Define a touch pointer by its hash:

 // SmallDraw func pointerHashFromTouch(_ touch:UITouch) -> Int { return Unmanaged.passUnretained(touch).toOpaque().hashValue } 
0
source

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


All Articles