/
SLGSequencedTapGestureRecognizer.m
165 lines (129 loc) · 3.46 KB
/
SLGSequencedTapGestureRecognizer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// OKSequencedTapGestureRecognizer.m
//
// Created by Grace Steven on 7/6/11.
// Copyright 2011 works5.com All rights reserved.
//
#import "SLGSequencedTapGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation SLGSequencedTapGestureRecognizer{
// private
NSInteger _currentLocationIndex;
CGPoint _invalidPoint;
}
#pragma mark INIT
//
//
//
-(id)initWithTarget:(id)target action:(SEL)action{
self = [super initWithTarget:target action:action];
if(self){
_pointMargin = 200;
_currentLocationIndex =0;
}
return self;
}
#pragma mark - Public
//
//
//
-(NSInteger)currentLocationIndex{
return _currentLocationIndex;
}
#pragma mark - Internal
//
//
//
-(void)reset{
[super reset];
_currentLocationIndex = 0;
}
//
// Calculate a rect given a point using the pointMargin
//
-(CGRect)_calculateRectForLocation:(CGPoint)aPoint{
CGRect aRect =
CGRectMake(aPoint.x-(_pointMargin/2),
aPoint.y-(_pointMargin/2),
_pointMargin,
_pointMargin);
return aRect;
}
//
//
//
-(BOOL)_testTouchPoint:(CGPoint)aTouchPoint{
CGPoint aLocationPoint = [self touchPointAtIndex:_currentLocationIndex];
BOOL isZeroPoint = CGPointEqualToPoint(aLocationPoint, CGPointZero);
if(!isZeroPoint){
CGRect aTouchRect = [self _calculateRectForLocation:aLocationPoint];
if(CGRectContainsPoint(aTouchRect,aTouchPoint)){
return YES;
}
}
return NO;
}
#pragma mark - Touch Handling
//
//
//
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
CGPoint aTouchPoint = [self locationInView:self.view];
BOOL result = [self _testTouchPoint:aTouchPoint];
if(!result)
[self setState:UIGestureRecognizerStateFailed];
}
//
//
//
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
CGPoint aTouchPoint = [self locationInView:self.view];
BOOL result = [self _testTouchPoint:aTouchPoint];
if(!result)
[self setState:UIGestureRecognizerStateFailed];
}
//
//
//
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
CGPoint aTouchPoint = [self locationInView:self.view];
BOOL result = [self _testTouchPoint:aTouchPoint];
if(!result){
[self setState:UIGestureRecognizerStateFailed];
}
else{
// increment the index
_currentLocationIndex++;
// reconize if reached the end of locations array
if(_currentLocationIndex >= [self numberOfRequiredTapLocations])
[super setState:UIGestureRecognizerStateRecognized];
}
}
//
//
//
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
[self setState:UIGestureRecognizerStateFailed];
}
#pragma mark - Hooks / Required Subclass Overrides
//
//
//
-(CGPoint)touchPointAtIndex:(NSInteger)anIndex{
[NSException raise:NSGenericException
format:@"Failed to override: %s",__PRETTY_FUNCTION__];
return CGPointZero;
}
//
//
//
-(NSUInteger)numberOfRequiredTapLocations{
[NSException raise:NSGenericException
format:@"Failed to override: %s",__PRETTY_FUNCTION__];
return 0;
}
@end