-
Notifications
You must be signed in to change notification settings - Fork 107
/
SSWDirectionalPanGestureRecognizer.m
50 lines (38 loc) · 1.5 KB
/
SSWDirectionalPanGestureRecognizer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// SSWDirectionalPanGestureRecognizer.m
//
// Created by Arkadiusz Holko http://holko.pl on 01-06-14.
//
#import "SSWDirectionalPanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface SSWDirectionalPanGestureRecognizer()
@property (nonatomic) BOOL dragging;
@end
@implementation SSWDirectionalPanGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) return;
CGPoint velocity = [self velocityInView:self.view];
// check direction only on the first move
if (!self.dragging && !CGPointEqualToPoint(velocity, CGPointZero)) {
NSDictionary *velocities = @{
@(SSWPanDirectionRight) : @(velocity.x),
@(SSWPanDirectionDown) : @(velocity.y),
@(SSWPanDirectionLeft) : @(-velocity.x),
@(SSWPanDirectionUp) : @(-velocity.y)
};
NSArray *keysSorted = [velocities keysSortedByValueUsingSelector:@selector(compare:)];
// Fails the gesture if the highest velocity isn't in the same direction as `direction` property.
if ([[keysSorted lastObject] integerValue] != self.direction) {
self.state = UIGestureRecognizerStateFailed;
}
self.dragging = YES;
}
}
- (void)reset
{
[super reset];
self.dragging = NO;
}
@end