【问题标题】:Overriding touchesBegan: Error - method does not override any method from superclass覆盖 touchesBegan:错误 - 方法不会覆盖超类中的任何方法
【发布时间】:2016-01-29 04:44:08
【问题描述】:

我正在尝试在 UITapGestureRecognizer 的自定义子类中覆盖 touchesBegan。代码如下。我从这里得到它:How to accelerate the identification of a single tap over a double tap?。这是公认的答案,但我收到一个错误:方法不会覆盖其超类中的任何方法。我已经检查过了,这确实似乎是 touchesBegan 的签名。帮忙?

import UIKit

class UIShortTapGestureRecognizer: UITapGestureRecognizer {
    let tapMaxDelay: Double = 0.3

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        delay(tapMaxDelay) {
        // Enough time has passed and the gesture was not recognized -> It has failed.
            if  self.state != UIGestureRecognizerState.Ended {
                self.state = UIGestureRecognizerState.Failed
            }
        }
    }


}

【问题讨论】:

    标签: ios swift uigesturerecognizer uitapgesturerecognizer


    【解决方案1】:

    问题源于您没有使用 Xcode 7;您的语法在 Swift 1.2+ 中,但您使用的 Xcode 版本不支持。

    如果您使用 Xcode 版本和 1.2 之前的 Swift 版本,则需要更改方法签名,如下所示:

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

    或者,升级到 Xcode 7。

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

    仅在 Xcode 6.3 之后才可用。

    【讨论】:

    • 这对我有用。我正在阅读几年前使用 Set&lt;NSObject&gt; 的教程,但正如您所描述的,现在 sig 是 Set&lt;UITouch&gt;。有用的注释here:'通常,当您尝试从 Apple 的框架之一覆盖或实现委托方法并获得“无效的重新声明...”或“方法未覆盖其超类中的任何方法”错误和没有明显的拼写错误,这意味着 Apple 更改了 API,并且通常参数的类型已更改。'
    【解决方案2】:

    适用于 Swift 4.2 和 XCode 10.2

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesBegan(touches, with: event)
    
        }
    

    【讨论】:

      【解决方案3】:

      请尝试删除 override 关键字。当你的超类也实现了该方法时,覆盖是可能的。在您的情况下,您正在提供一个替换或修改超类实现行为的版本。我认为这里没有采取行动。

      另一方面,如果您没有使用 swift 的升级版本,例如 swift 2.0+,那么请升级您的 Xcode,这也会升级您的 Swift 版本。

      在我的例子中,这个方法如下所示。我将 Xcode 7.2 与 Swift 2.1.1 一起使用。只是作为一个例子 -

      override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
          testTouchMan(touches)
          let touch: UITouch! = touches.first as UITouch!
          let location = touch.locationInView(self.groundFloorView)
      }
      

      【讨论】:

        【解决方案4】:

        我确实在使用 Xcode 7.2 和 Swift 2.0。并且删除覆盖键盘不是解决方案。相反,我发现解决方案是添加导入 UIKit.UIGestureRecognizerSubclass。这也让我可以改写 state 属性,而不是只读的。

        【讨论】:

          猜你喜欢
          • 2016-02-20
          • 1970-01-01
          • 1970-01-01
          • 2016-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多