2014-10-04 93 views
2
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    for (UITouch *aTouch in touches) { 
     if (aTouch.tapCount >= 2) { 
      // The view responds to the tap 
     } 
    } 
} 

我使用上面的代碼檢測雙擊手勢;然而,我怎樣才能設置代碼只發生一次?僅檢測一次雙擊手勢一次

換句話說,當你點擊一次,字符跳轉。當你連續兩次點擊時,角色會雙跳。但是,你如何設置水龍頭,使得角色不會連續雙跳並且從單視圖上走得更高,而不需要最初錄製一次

回答

1

實現此目的的一種非常簡單的方法是聲明一個全局變量bool,並在檢測到雙擊後設置它的值!

事情是這樣的:

@interface MyViewController() 
{ 
    bool isTapped; 
} 
@end 

@implementation MyViewController 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *aTouch in touches) { 
     if (aTouch.tapCount >= 2) { 
      if(!isTapped) { 
       // The view responds to the tap 
       isTapped = YES; 
      } 
     } 
    } 
} 
@end 

希望這有助於

0

不知道這是否可以幫助,也只取一個標誌,並設置它是或否因此: -

UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
tapRecognizer.delegate = self; 
tapRecognizer.minimumPressDuration = //Up to you; 
[self.someView addGestureRecognizer:tapRecognizer];