我正在尝试覆盖 UITapGestureRecognizer 的自定义子类中的 touchesBegan。代码如下。我从这里得到它:How to accelerate the identification of a single tap over a double tap? .这是公认的答案,但我收到一个错误:方法不会覆盖其父类(super class)中的任何方法。我已经检查过了,这似乎确实是 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
}
}
}
}
最佳答案
问题源于您没有使用 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 开始可用。
关于ios - 覆盖 touchesBegan : Error - method does not override any method from superclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077042/