是否可以同时使用UIPanGesture和UISwipeGesture? [英] Is it possible to have a UIPanGesture and UISwipeGesture together?

查看:100
本文介绍了是否可以同时使用UIPanGesture和UISwipeGesture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIIMageView子类,并且同时添加了一个UIPanGestureRecognizer和一个UISwipeGestureRecognizer,如下所示:

I have a UIIMageView subclass and I am adding both a UIPanGestureRecognizer and a UISwipeGestureRecognizer as follows:

self.userInteractionEnabled = YES;

UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[self addGestureRecognizer:panGesture];

UISwipeGestureRecognizer * swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeUp:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self addGestureRecognizer:swipeUp];

UISwipeGestureRecognizer * swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:swipeDown];

但是当我向上或向下滑动选择器时不会被调用,但是平底锅总是会被调用.

but when I swipe up or down my selectors do not get called but the pan always does.

任何澄清都是有帮助的.

Any clarification would be helpful.

谢谢

推荐答案

问题在于,滑动手势也将被识别为panGesture.

The problem is that the swipe gesture will be recognized as a panGesture too.

您需要做的是将委托设置为实现该方法的类:

What you need to do is set the delegate to a class that implement the method:

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

这将允许您同时识别两个手势,但需要向您清楚一点,即始终在调用滑动时也会调用平移.

This will allow you to recognize the two gestures simultaneous, but need to be clear to you that always when your swipe is called your pan will called too.

当通过gestureRecognizer或otherGestureRecognizer识别手势将阻止另一个手势识别器识别其手势时,将调用此方法.请注意,返回YES可以保证同时识别.另一方面,不能保证返回NO会阻止同时识别,因为另一个手势识别器的委托人可能会返回YES.

This method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer would block the other gesture recognizer from recognizing its gesture. Note that returning YES is guaranteed to allow simultaneous recognition; returning NO, on the other hand, is not guaranteed to prevent simultaneous recognition because the other gesture recognizer's delegate may return YES.

这篇关于是否可以同时使用UIPanGesture和UISwipeGesture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆