Iphone SDK中的同时手势识别器

我需要使用UISwipeGestureRecognizer (例如, UISwipeGestureRecognizerDirectionRightUISwipeGestureRecognizerDirectionLeft )来捕捉两个不同的手势。 当我用addGestureRecognizer方法添加两个不同的识别器时,只有最后添加的识别器工作。 我读过,我需要实现的gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizerDelegate协议的方法,但没有任何工作。

任何人都可以帮助捕捉两个或更多相同的手势简单的例子? 谢谢!

这很简单:

首先,我们应该创build类,它实现了UIGestureRecognizerDelegate协议:

 @interface MyGestureDelegate : NSObject <UIGestureRecognizerDelegate> 
@implementation MyGestureDelegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return YES; }
@implementation MyGestureDelegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return YES; } 

像这样使用它:

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureLeft:)]; [self.view addGestureRecognizer:swipeGestureLeft]; swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; [swipeGestureLeft release]; UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeGesture]; MyGestureDelegate *deleg = [[MyGestureDelegate alloc] init]; [swipeGesture setDelegate:deleg]; [swipeGesture release]; 

答案是:“呃,快看文档…”绝对不行!

他正在设置一个掩码,然后使用相同的variables来进行testing,就像识别器清除它并设置一个位一样。 它存储,正如他从文档中正确引用:

滑动允许的方向

 sender.direction 

将简单地返回你最初设置的面具,并在他的例子,永远不会解决一个方向!

 UISwipeGestureRecognizerDirectionRight == 1 UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft == 3 

另外,对于大多数情况,您不需要:

  • 设置一个委托
  • 允许同时手势识别(除非你想同时刷卡;不太可能)
  • 将识别器发送到select器

以下为我工作:

  UISwipeGestureRecognizer* swipe; swipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeL)] autorelease]; swipe.direction = UISwipeGestureRecognizerDirectionLeft; [view addGestureRecognizer:swipe]; swipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeR)] autorelease]; swipe.direction = UISwipeGestureRecognizerDirectionRight; // default [view addGestureRecognizer:swipe]; 

呃,快速浏览一下文档就会发现你正在做的工作比你需要的要多:

“方向滑动的允许方向。

 @property(nonatomic) UISwipeGestureRecognizerDirection direction 

讨论您可以通过使用按位或操作数指定多个UISwipeGestureRecognizerDirection常量来指定多个方向。 默认的方向是UISwipeGestureRecognizerDirectionRight 。“

也就是说,而不是使用两个UISwipeGestureRecognizers,您可以执行以下操作:

 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; 

并在你的行动方法:

  -(IBAction)handleSwipeGesture:(UISwipeGestureRecognizer*)sender { if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { //do left action } else { //do right action } } 

更简单,更不容易发生冲突。

我build议你阅读一下手势识别器用来识别手势的技巧。 我猜想,第一个识别者试图识别这个姿势,但是却意识到他不必对此做出回应,然后不知何故,他不会将其传递出去。

阅读他们如何工作以了解如何使用它们是非常有用的。

希望这可以帮助。

感谢user294890的build议,我通过列出的UIGestureRecognizerDelegate方法返回yes而成功。

然而,我发现,当我在Web视图加载之前添加GestureRecognizer时,它不起作用。 但是,当我在webView委托方法中将GestureRecognizer添加到webView时,它确实有效:

- (void)webViewDidFinishLoad:(UIWebView *)webView