26

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

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

4

5 回答 5

35

这真的很容易:

首先我们应该创建实现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;
}

并像这样使用它:


    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];

于 2010-04-13T10:30:07.973 回答
20

凤凰城的回答:“嗯,快速浏览一下文档……”绝对行不通!

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

允许的滑动方向

sender.direction

将简单地返回您最初设置的掩码,在他的示例中,永远不会解析为单个方向!

UISwipeGestureRecognizerDirectionRight == 1
UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft == 3

此外,在大多数情况下,您不需要:

  • 设置委托
  • 允许同时进行手势识别(除非您想要同时滑动;不太可能)
  • 将识别器发送到选择器

以下对我有用:

   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];
于 2010-07-23T15:06:58.760 回答
2

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

"direction 允许的滑动方向。

@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

     }
}

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

于 2010-06-24T02:38:57.997 回答
1

我建议您阅读一些有关手势识别器用于识别手势的技术。我想,第一个识别器尝试识别手势,但意识到他不必响应它,然后不知何故他没有传递它。

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

希望这可以帮助。

于 2010-04-13T08:19:17.573 回答
0

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

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

- (void)webViewDidFinishLoad:(UIWebView *)webView.

于 2012-07-11T03:09:34.743 回答