小编典典

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

swift

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

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)。所以我的问题是,如何获得第二(和第三,第四…)触摸的坐标?


阅读 369

收藏
2020-07-07

共1个答案

小编典典

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

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

self.view.isMultipleTouchEnabled = true

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

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