Skip to content

Instantly share code, notes, and snippets.

@ruandao
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save ruandao/e3d820347fce0bf32331 to your computer and use it in GitHub Desktop.

Select an option

Save ruandao/e3d820347fce0bf32331 to your computer and use it in GitHub Desktop.
DirectionPanGestureRecognizer 专门识别竖向或者横向的拖动
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
typedef enum {
DirectionPangestureRecognizerVertical,
DirectionPanGestureRecognizerHorizontal
} DirectionPangestureRecognizerDirection;
@interface DirectionPanGestureRecognizer : UIPanGestureRecognizer
@property (nonatomic, assign) DirectionPangestureRecognizerDirection direction;
@property (assign, nonatomic) BOOL hadRecognizer;
@end
#import "DirectionPanGestureRecognizer.h"
@interface DirectionPanGestureRecognizer ()
@end
@implementation DirectionPanGestureRecognizer
@synthesize direction = _direction;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) return;
if (self.hadRecognizer == YES) {
return;
}
CGPoint nowPoint = [[touches anyObject] locationInView:self.view];
CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view];
CGFloat moveX = prevPoint.x - nowPoint.x;
CGFloat moveY = prevPoint.y - nowPoint.y;
if (self.direction == DirectionPangestureRecognizerVertical) {
if (fabs(moveX) > fabs(moveY)) {
self.state = UIGestureRecognizerStateFailed;
} else if(fabs(moveY) > fabs(moveX)) {
self.hadRecognizer = YES;
}
} else {
if (fabs(moveX) < fabs(moveY)) {
self.state = UIGestureRecognizerStateFailed;
} else if(fabs(moveY) < fabs(moveX)) {
self.hadRecognizer = YES;
}
}
}
- (void)reset {
[super reset];
self.hadRecognizer = NO;
}
@end
    #import "DirectionPanGestureRecognizer.h"
    
    ...
    
    DirectionPanGestureRecognizer *verticalPanGestReco = [[DirectionPanGestureRecognizer alloc] initWithTarget:self action:@selector(panG:)];
    verticalPanGestReco.direction = DirectionPangestureRecognizerVertical;
    [self addGestureRecognizer:verticalPanGestReco];
    
    #pragma mark - 竖向手势识别
    - (void)panG:(DirectionPanGestureRecognizer*)gR
    {
            switch (gR.state) {
                    case UIGestureRecognizerStateBegan:
            
                            break;
                    case UIGestureRecognizerStateChanged:
                            if (gR.hadRecognizer) {
                                    // ...
                            }
                            break;
                    case UIGestureRecognizerStateEnded:
                            break;
                    default:
                            break;
            }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment