是否可以将Pan Gesture和Swipe Gesture结合在一起?

时间:2012-03-28 12:39:27

标签: iphone objective-c xcode ipad

我有一个UIIMageView子类,我正在添加UIPanGestureRecognizer和UISwipeGestureRecognizer,如下所示:

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

但是当我向上或向下滑动时,我的选择器不会被调用,但是泛有效。

任何澄清都会有所帮助。

由于

1 个答案:

答案 0 :(得分:3)

问题是滑动手势也会被识别为panGesture。

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

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

这将允许您同时识别这两个手势,但需要清楚地知道,当您的滑动被称为平移时,您的手柄也会被调用。

From apple documentation

  

当gestureRecognizer或otherGestureRecognizer识别手势会阻止其他手势识别器识别其手势时,会调用此方法。请注意,保证返回YES可以同时识别;另一方面,返回NO不能保证防止同时识别,因为其他手势识别器的代表可能会返回YES。