Created
October 8, 2014 07:23
-
-
Save coordinates/e0c82349992a068526e1 to your computer and use it in GitHub Desktop.
This file contains 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 "FlickGestureRecognizer.h" | |
#import <UIKit/UIGestureRecognizerSubclass.h> | |
@implementation FlickGestureRecognizer | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
[super touchesBegan:touches withEvent:event]; | |
_direction = FlickGestureRecognizerDirectionNone; | |
if (1 == [touches count]) { | |
self.state = UIGestureRecognizerStateBegan; | |
return; | |
} | |
} | |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
[super touchesMoved:touches withEvent:event]; | |
UITouch *touch = [touches anyObject]; | |
CGPoint location = [touch locationInView:self.view]; | |
if(CGRectContainsPoint(self.view.bounds, location)) { | |
_direction = FlickGestureRecognizerDirectionNone; | |
} | |
else { | |
CGFloat x = location.x - CGRectGetWidth(self.view.frame) / 2; | |
CGFloat y = location.y - CGRectGetHeight(self.view.frame) / 2; | |
CGFloat radians = atan2f(y, x); | |
if (radians < 0) { | |
radians = M_PI * 2 + radians; | |
} | |
CGFloat degree = radians * 180 / M_PI; | |
_direction = [self directionFromDegree:degree]; | |
} | |
} | |
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
_direction = FlickGestureRecognizerDirectionNone; | |
self.state = UIGestureRecognizerStateCancelled; | |
} | |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
self.state = UIGestureRecognizerStateRecognized; | |
} | |
- (void)reset { | |
[super reset]; | |
_direction = FlickGestureRecognizerDirectionNone; | |
} | |
- (FlickGestureRecognizerDirection)directionFromDegree:(CGFloat)degree | |
{ | |
FlickGestureRecognizerDirection direction = FlickGestureRecognizerDirectionNone; | |
CGFloat offsetDegree = fmod((degree + 45.0f), 360.0f); | |
if(90.0f > offsetDegree) { | |
direction = FlickGestureRecognizerDirectionRight; | |
} | |
else if(180.0f > offsetDegree) { | |
direction = FlickGestureRecognizerDirectionDown; | |
} | |
else if(270.0f > offsetDegree) { | |
direction = FlickGestureRecognizerDirectionLeft; | |
} | |
else { | |
direction = FlickGestureRecognizerDirectionUp; | |
} | |
return direction; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment