在Swift中获取多个触摸的坐标

所以我一直在试图获得触摸屏的坐标。 到目前为止,我可以通过这个获得一个触摸的坐标:

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

但是当用两根手指触摸时,我只能得到第一次触摸的坐标。 多点触摸的作品(我用这个小教程testing: http : //www.techotopia.com/index.php/An_Example_Swift_iOS_8_Touch,_Multitouch_and_Tap_Application )。 所以我的问题是,我如何获得第二(和第三,第四…)触摸的坐标?

给定的touches参数是一组检测到的触摸。 您只能看到一个触摸,因为您select触摸之一:

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

为了得到所有的触摸迭代给定的集合

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

**更新为Swift 4和Xcode 9(2017年10月8日)**

首先, 记住通过设置启用多点触摸事件

 self.view.isMultipleTouchEnabled = true 

在你的UIViewController的代码中,或者在Xcode中使用适当的storyboard选项:

xcode截图

否则,你总会在touchesBegan获得一个单一的触摸( 见文档 )。

然后,在touchesBegan里面,迭代一组触摸来获得它们的坐标:

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

你必须迭代不同的触摸。 这样你可以访问每一个接触。

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

在Swift 1.2中已经改变了, touchesBegan现在提供了一套NSObject。 要遍历它们,将touches集合转换为一组UITouch对象,如下所示:

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

对于Swift 3,根据@ Andrew的回答:

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

编辑,我的坏,这不是回答你的问题,有同样的问题,有人把我连接到这个以前的答案 :

无论如何,我不得不改变一些东西,使其在迅速3,这是我目前的代码:

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

我仍然有一个小问题,但我认为它与我的代码中的我的GestureRecognizer相关联。 但是,这应该做的伎俩。 它会打印你的CONSOL中每个点的坐标。

在Swift中3,4通过哈希来识别触摸指针:

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