#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;
}
}
Last active
August 29, 2015 14:02
-
-
Save ruandao/e3d820347fce0bf32331 to your computer and use it in GitHub Desktop.
DirectionPanGestureRecognizer 专门识别竖向或者横向的拖动
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 <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 |
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" | |
| @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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment