获取Swift中多个触摸的坐标

Fr4*_*0NL 7 coordinates ios swift

所以我一直在试图获取屏幕上的触摸坐标.到目前为止,我可以通过以下方式获得一次触摸的坐标:

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

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

Par*_*ara 15

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

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

self.view.isMultipleTouchEnabled = true
Run Code Online (Sandbox Code Playgroud)

在你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)
    }
}
Run Code Online (Sandbox Code Playgroud)