【问题标题】:Swift 2.1: how to override touchesBegan & touchesMoved [duplicate]Swift 2.1:如何覆盖 touchesBegan 和 touchesMoved [重复]
【发布时间】:2016-01-28 05:08:27
【问题描述】:

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

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

那么我是否应该覆盖超类中的函数?

【问题讨论】:

  • 您能否扩展代码的... 部分。
  • 对不起,我的意思是填写签名,例如 touchesBegan(...
  • 试试override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)

标签: ios swift overriding superclass touchesbegan


【解决方案1】:

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

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

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多