2016-01-29 143 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) 
}