覆盖Swift中的touchesBegan

时间:2014-11-07 23:27:13

标签: ios swift

我压倒一切:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)

在斯威夫特。

我想知道,获得一个接触点是否正确:

var touchPoint: CGPoint! = event.touchesForView(self)?.anyObject()?.locationInView(self)

提前致谢!

1 个答案:

答案 0 :(得分:2)

从iOS 8.3开始,touchesBegan采用touches参数,即Set<NSObject>。它现在是Set<UITouch>。所以,你会:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let point = touches.first?.location(in: view) else { return }

    // use `point` here
}

在以前的iOS版本中,touchesBegan被定义为NSSet参数,如问题中所述,因此您可以:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    if let touch = touches.anyObject() as? UITouch {
        let touchPoint = touch.locationInView(view)
        ...
    }
}