2016-01-29 144 views
0

我想覆盖UITapGestureRecognizer的自定义子类中的touchesBegan。代码如下。我从这里得到它:How to accelerate the identification of a single tap over a double tap?。这是公认的答案,但我得到一个错误:方法不会覆盖任何超类的方法。我已经检查过,这确实似乎是touchesBegan的签名。帮帮我?覆盖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 
      } 
     } 
    } 


} 

回答

0

我确实使用Xcode 7.2与Swift 2.0。并且删除覆盖键盘不是解决方案。相反,我发现解决方案是添加导入UIKit.UIGestureRecognizerSubclass。这也允许我写下状态属性,而不是只读状态。

3

该问题源于您未使用Xcode 7的事实;你的语法是在Swift 1.2+中,但不支持你正在使用的Xcode版本。

如果您使用的Xcode版本雨燕之前的版本1.2,您将需要改变方法的签名如下:

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

或者,升级到Xcode的7

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

成为仅在Xcode 6.3之后才可用。

0

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

另一方面,如果你不使用swift的升级版本像swift 2.0+那么请升级你的Xcode,它也会升级你的Swift版本。

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

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