2013-08-05 174 views
4

我在我的UIView上使用兩個手勢識別器。一個是標準UITapGestureRecognizer,另一個是很簡單的觸摸下來識別我寫道:同時使用觸摸和觸摸手勢識別器

@implementation TouchDownGestureRecognizer 

- (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; 
} 

@end 

他們一起工作只有當我分配一個委託他們兩個包含此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    return YES; 
} 

一切正常沒問題,但是當我長時間按下該視圖時,觸摸識別器觸發並觸摸識別器不觸發。對於短小的水龍頭,一切都很好,他們都開火。

我實現了UIGestureRecognizerDelegate中的所有方法來返回YES無濟於事。如果我添加日誌記錄以查看與代理的交互並在我自己的識別器中進行交互,則可以看到,對於短時間和長時間點擊,調用序列是相同的 - 除了觸​​摸識別器的調用。我做錯了什麼?

回答

6

爲什麼不直接從UILongPressGestureRecognizer檢查touchUp?

-(void)selectionDetected:(UILongPressGestureRecognizer*)longPress 
{ 
    if(longPress.state==1) 
    { 
     //long Press is being held down 
    } 
    else if(longPress.state==3) 
    { 
     //the touch has been picked up 
    } 
} 
+1

很好的建議,謝謝!該識別器需要'minimumPressDuration'設置爲0,現在工作正常 –

+0

沒問題,很高興幫助! –