Swift 2.1:如何覆盖touchesBegan & touchesMoved

Swift 2.1: how to override touchesBegan & touchesMoved

当我这样声明这两个函数时,我的行为非常奇怪:

override func touchesBegin(touches: NSSet, withEvent event: UIEvent)
override func touchesMoved(touches: NSSet, withEvent event: UIEvent)

我收到错误消息:

Method does not override any method from its superclass

然而,当我这样声明它们时:

func touchesBegin(touches: NSSet, withEvent event: UIEvent)
func touchesMoved(touches: NSSet, withEvent event: UIEvent)

我收到错误消息:

Method 'touchesBegan(_:withEvent:)' with Objective-C selector 'touchesBegan:withEvent:' conflicts with method 'touchesBegan(_:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector

Method 'touchesMoved(_:withEvent:)' with Objective-C selector 'touchesMoved:withEvent:' conflicts with method 'touchesMoved(_:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector

那么我是否重写超类中的函数?

我认为您正在寻找的方法签名是:

class XXX : UIResponder {

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {

    }
}