自定义UIPanGestureRecognizer只能垂直或水平滑动

Author Avatar
killua167 12月 29, 2016

如何自定义UIPanGestureRecognizer,让它只能响应垂直或水平滑动。

场景

有点类似网易新闻首页,一个tableView上某些cell需要左右滑动手势进行操作,而tableView又作为scrollView的子视图可以左右滑动。这时,cell的手势就和scrollView的冲突了,导致cell只响应UIPanGestureRecognizer手势,而无法垂直滑动。这就需要屏蔽自定义手势的上下滑动,只保留左右滑动。

动手

那就先从UIPanGestureRecognizer的代理方法入手,看有没办法解决这个手势冲突。百度上大部分都是这个方法来判断,然而我试验了一下,这个是直接取消其中一个的手势响应,和我需求不服,pass!

// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

再尝试其他方法

// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

看了下API说明,这个方法会在Pan手势介入事件之前(即gestureRecognizerShouldBegin方法之前)发生,来判断是否让Pan手势响应。但是这里
gestureRecognizer的state都是UIGestureRecognizerStatePossible,即还没识别,所以不能预测到滑动的方向 ,显然无法解决。

根源

问题的解决办法根源在​gestureRecognizerShouldBegin执行之前获知它的滑动方向,这样就能提前判断是不是让Pan接受响应,所以我们必须用更底层的方法。

解决

在stackOverFlow里发现了这个帖子:
http://stackoverflow.com/questions/7100884/uipangesturerecognizer-only-vertical-or-horizontal​

看到一个大神的回答,它的办法是 写一个继承UIPanGestureRecognizer的 PanGestureRecognizer,然后重写

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

这个方法来判断方向,这个方法较底层,它会比gestureRecognizerShouldBegin更早的得到相应,在这个方法里去让Pan不响应就好了。

最后附上代码:

#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

typedef enum {
    DirectionPangestureRecognizerVertical,
    DirectionPanGestureRecognizerHorizontal
} DirectionPangestureRecognizerDirection;

@interface DirectionPanGestureRecognizer : UIPanGestureRecognizer {
    BOOL _drag;
    int _moveX;
    int _moveY;
    DirectionPangestureRecognizerDirection _direction;
}

@property (nonatomic, assign) DirectionPangestureRecognizerDirection direction;

@end
#import "DirectionPanGestureRecognizer.h"

int const static kDirectionPanThreshold = 5;

@implementation DirectionPanGestureRecognizer

@synthesize direction = _direction;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    if (self.state == UIGestureRecognizerStateFailed) return;
    CGPoint nowPoint = [[touches anyObject] locationInView:self.view];
    CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view];
    _moveX += prevPoint.x - nowPoint.x;
    _moveY += prevPoint.y - nowPoint.y;
    if (!_drag) {
        if (abs(_moveX) > kDirectionPanThreshold) {
            if (_direction == DirectionPangestureRecognizerVertical) {
                self.state = UIGestureRecognizerStateFailed;
            }else {
                _drag = YES;
            }
        }else if (abs(_moveY) > kDirectionPanThreshold) {
            if (_direction == DirectionPanGestureRecognizerHorizontal) {
                self.state = UIGestureRecognizerStateFailed;
            }else {
                _drag = YES;
            }
        }
    }
}

- (void)reset {
    [super reset];
    _drag = NO;
    _moveX = 0;
    _moveY = 0;
}

@end