是否可以同时使用UIPanGesture和UISwipeGesture? [英] Is it possible to have a UIPanGesture and UISwipeGesture together?
问题描述
我有一个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.