Swift 2.1:如何覆盖touchesBegan& touchesMoved

时间:2015-10-28 12:16:34

标签: ios swift override superclass touchesbegan

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

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

我是否要覆盖超类中的函数?

1 个答案:

答案 0 :(得分:2)

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

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?) {

    }
}