2016-01-21 20 views
0

我正在嘗試爲我的視圖創建自定義手勢識別器。我在這裏提到這個答案:但由於某種原因,被觸及的結束,也觸及movied沒有得到調用。只有接觸開始被調用。觸摸使用UIGestureRecogniser進行子類化時未結束調用

子類

#import "TapGesture.h" 
#import <UIKit/UIGestureRecognizerSubclass.h> 

@implementation TapGesture 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if (self.state == UIGestureRecognizerStatePossible) { 
     self.state = UIGestureRecognizerStateRecognized; 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    self.state = UIGestureRecognizerStateFailed; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    self.state = UIGestureRecognizerStateFailed; 
} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ 

} 

@end 

,我初始化的TapGesture如下:

TapGesture *tapGesture=[[TapGesture alloc]initWithTarget:self action:@selector(incrementValue)]; 
tapGesture.delaysTouchesEnded=NO; 
tapGesture.cancelsTouchesInView=NO; 
[_workButton addGestureRecognizer:tapGesture]; //_workButton is a UIView 

我不; t有視圖中的任何其他手勢識別器。如果我在UIView中使用相同的方法,所有這些都按預期方式調用。

爲什麼touchesEnded/touchesMoved在UIGestureRecogniser類中重寫時沒有被調用?

回答

4

當繼承UIGestureRecognizer你必須讓它像一個連續姿態和處理它的狀態機由自己(即手動設置它的state)。

從iOS開發者庫文件上UIGestureRecognizer

子類必須的狀態屬性設置爲當他們狀態之間轉換的適當的值。

See here for more info(向下滾動到Subclasing注意

  • 注意:爲了讓state讀/寫,而不是隻讀的,你應該使用UIGestureRecognizerSubclass.h作爲還注意到文檔:

    state屬性在UIGestureRecognizer.h中聲明爲只讀。此屬性聲明專供手勢識別器的客戶使用。 UIGestureRecognizer的子類必須導入UIGestureRecognizerSubclass.h。這個頭文件包含一個使其可讀寫的狀態重新聲明。