重写触摸开始在Swift中

我压倒一切:

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

在Swift中。

我想知道,这是正确的一个接触点:

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

提前致谢!

从iOS 8.3开始, touchesBegan接受了一个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) ... } }