Created
April 16, 2013 03:24
-
-
Save ifournight/5393129 to your computer and use it in GitHub Desktop.
DirectionPanGestureRecognizer
From http://goo.gl/H65QC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment