Iphone SDK 中的同步手势识别器

发布于 2024-08-29 04:38:48 字数 397 浏览 3 评论 0原文

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

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

I need to catch two different swipping gestures using UISwipeGestureRecognizer(for example, UISwipeGestureRecognizerDirectionRight and UISwipeGestureRecognizerDirectionLeft). When I add two different recognisers with addGestureRecognizer method, only last added recognizer works. I've read that I need to implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method of UIGestureRecognizerDelegate protocol, but nothing works.

Can anyone help with simple example of catching two or more same gestures?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦毁影碎の 2024-09-05 04:38:48

这非常简单:

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

It was really easy:

At first we should create class, that implements UIGestureRecognizerDelegate protocol:

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

And use it like this:


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

故人的歌 2024-09-05 04:38:48

答案:
“嗯,快速浏览一下文档...”来自 Phoenix 绝对行不通!

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

允许的滑动方向

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

The answer:
"Um, a quick look at the documentation..." from Phoenix absolutely will not work!

He is setting a mask, then using the same variable to test as if the recognizer cleared it and set a single bit. It stores, as he correctly quoted from the documentation:

The permitted directions of the swipe

sender.direction

will simply return the mask you set initially and in his example, will never resolve to a single direction!

UISwipeGestureRecognizerDirectionRight == 1
UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft == 3

Additionaly, for most cases you don't need to:

  • setup a delegate
  • permit simultaneous gesture recognition (unless you want simultaneous swipes; not likely)
  • send the recognizer to the selector

The following works for me:

   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];
寄居者 2024-09-05 04:38:48

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

“方向
允许的滑动方向。

@property(nonatomic) UISwipeGestureRecognizerDirection direction

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

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

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

     }
}

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

Um, a quick look at the documentation would reveal that you are doing way more work than you need to:

"direction
The permitted directions of the swipe.

@property(nonatomic) UISwipeGestureRecognizerDirection direction

Discussion
You may specify multiple directions by specifying multiple UISwipeGestureRecognizerDirection constants using bitwise-OR operands. The default direction is UISwipeGestureRecognizerDirectionRight."

Which is to say, instead of using two UISwipeGestureRecognizers, you can just do the following:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(handleSwipeGesture:)];

 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

And in your action method:

  -(IBAction)handleSwipeGesture:(UISwipeGestureRecognizer*)sender
     {
     if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {

         //do left action

     } else {

         //do right action

     }
}

Much simpler, and much less prone to conflict.

Oo萌小芽oO 2024-09-05 04:38:48

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

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

希望这有帮助。

I would suggest you read a little bit on the technique that the gesture recognizers use to recognize the gesture. I suppose, the first recognizer tries to recognize the gesture, but realizes that he does not has to respond to it and then somehow he does not pass it on.

It's very useful to read how they work in order to understand how to use them.

Hope this helps.

方圜几里 2024-09-05 04:38:48

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

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

- (void)webViewDidFinishLoad:(UIWebView *)webView

Thanks to user294890's suggestion, I was successful by returning yes from the listed UIGestureRecognizerDelegate methods.

I found however that it would not work when I was adding the GestureRecognizer before the web view had loaded. It did however work when I added the GestureRecognizer to the webView in the webView delegate method:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文