所以我一直在四处寻找屏幕上触摸的坐标。到目前为止,我可以通过以下方式获得一触的坐标:

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


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

最佳答案

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

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

self.view.isMultipleTouchEnabled = true


在您的UIViewController代码中,或在Xcode中使用相应的故事板选项:

ios - 在Swift中获取多次触摸的坐标-LMLPHP

否则,您总是会在touchesBegansee documentation here)中获得单一触摸。

然后,在touchesBegan内部,迭代一组触摸以获取其坐标:

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

关于ios - 在Swift中获取多次触摸的坐标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48643628/

10-12 00:15
查看更多