-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PlopixKonamiGesture.swift
executable file
·260 lines (239 loc) · 7.17 KB
/
PlopixKonamiGesture.swift
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* This file is part of the PlopixKonamiGesture package.
*
* (c) Sébastien Morel aka Plopix <[email protected]>
*
* For the full copyright and license information(MIT), please view the LICENSE
* file that was distributed with this source code.
*/
import UIKit
import UIKit.UIGestureRecognizerSubclass
/**
Allow you to track the KomamiTouchCode
Up,up,Down,Down,Left,Right,Left,Right,Tap,Tap
We transform BA by Tap Tap for obvious simple reason
*/
class PlopixKonamiGesture: UIGestureRecognizer {
/**
Take care of the direction vectors
*/
struct Direction {
var up: CGVector
var down: CGVector
var left: CGVector
var right: CGVector
var neutral: CGVector
init() {
neutral = CGVectorMake( 0, 0 )
up = CGVectorMake( 0, -1 )
down = CGVectorMake( 0, 1 )
left = CGVectorMake( -1, 0 )
right = CGVectorMake( 1, 0 )
}
func isUp( vector: CGVector ) -> Bool {
return vector == self.up
}
func isDown( vector: CGVector ) -> Bool {
return vector == self.down
}
func isRight( vector: CGVector ) -> Bool {
return vector == self.right
}
func isLeft( vector: CGVector ) -> Bool {
return vector == self.left
}
func isNeutral( vector: CGVector ) -> Bool {
return vector == self.neutral
}
}
/**
Direction instance
*/
private let direction : Direction
/**
Configuration
*/
private let swipeDistanceTolerance : CGFloat = 50.0
private let swipeMinDistance : CGFloat = 50.0
/**
Store the Konami Code
*/
private var konamiCode: [CGVector] = []
/**
Store the user code
*/
private var currentCode : [CGVector] = []
/**
Store the starting point of the gesture
*/
private var startingPoint: CGPoint = CGPointZero
/**
init
*/
override init(target: AnyObject?, action: Selector) {
self.direction = Direction()
self.konamiCode = [
direction.up,direction.up,
direction.down,direction.down,
direction.left,direction.right,
direction.left,direction.right,
direction.neutral,direction.neutral]
super.init(target: target, action: action)
}
/**
Get the next vector
*/
private func nextVector() -> CGVector? {
let succeedGesture = currentCode.count
if ( succeedGesture == konamiCode.count) {
return nil;
}
return konamiCode[succeedGesture]
}
/**
Cancef if the user deviate
*/
private func isOnHisWay( point: CGPoint ) -> Bool {
let next: CGVector? = self.nextVector()
if (( next) == nil ) {
return true
}
if ( direction.isNeutral( next! ) ) {
return true;
}
let deltaX: CGFloat = point.x - startingPoint.x;
let deltaY: CGFloat = point.y - startingPoint.y;
// check the diversion
if ( direction.isUp( next! ) || direction.isDown( next! ) ) {
// next move in on Y, so we check X
if ( abs( deltaX ) > self.swipeDistanceTolerance ) {
return false;
}
}
if ( direction.isLeft( next! ) || direction.isRight( next! ) ) {
// next move in on X, so we check Y
if ( abs( deltaY ) > self.swipeDistanceTolerance ) {
return false;
}
}
// check the direction
if ( direction.isUp( next! ) ) {
if ( deltaY < 0 ) {
return true;
}
}
if ( direction.isDown( next! ) ) {
if ( deltaY > 0 ) {
return true;
}
}
if ( direction.isLeft( next! ) ) {
if ( deltaX < 0 ) {
return true;
}
}
if ( direction.isRight( next! ) ) {
if ( deltaX > 0 ) {
return true;
}
}
return false;
}
/**
We need at least a minimum distance
*/
private func hasReachMinDistance( point: CGPoint ) -> Bool {
let deltaX: CGFloat = point.x - startingPoint.x;
let deltaY: CGFloat = point.y - startingPoint.y;
let next: CGVector? = self.nextVector()
if (( next) == nil ) {
return true
}
if ( direction.isUp( next! ) || direction.isDown( next! ) ) {
if ( abs( deltaY ) > self.swipeMinDistance ) {
return true
}
}
if ( direction.isLeft( next! ) || direction.isRight( next! ) ) {
if ( abs( deltaX ) > self.swipeMinDistance ) {
return true
}
}
if ( direction.isNeutral( next! ) ) {
return true;
}
return false
}
/**
override method
This gesture doesn't prevent anything
*/
override func canPreventGestureRecognizer(preventedGestureRecognizer: UIGestureRecognizer) -> Bool {
return false;
}
/**
Touches Began
*/
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
if ( event.touchesForGestureRecognizer(self)?.count > 1 ) {
// give a direct failed when more than touches are detected
self.state = .Failed;
return
}
let touch:UITouch = touches.first!
self.startingPoint = touch.locationInView(self.view)
if ( self.state == .Changed ) {
// do nothing now
//@todo: add time check
return
}
if ( self.state == .Possible ) {
// only the first time
self.state = .Began;
return
}
self.state = .Failed;
}
/**
Touches Moved
*/
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
let touch:UITouch = touches.first!
if ( !self.isOnHisWay(touch.locationInView(self.view)) ) {
self.state = .Failed;
}
}
/**
Touches Ended
*/
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
let touch:UITouch = touches.first as UITouch!
let endPoint:CGPoint = touch.locationInView(self.view)
if ( self.isOnHisWay(endPoint) && self.hasReachMinDistance(endPoint) ) {
//go next or finish
let next: CGVector? = self.nextVector()
if (( next) != nil ) {
self.currentCode.append(next!)
}
if ( self.currentCode == self.konamiCode ) {
self.state = .Ended;
}
return
}
self.state = .Failed;
}
/**
Touches Cancelled
*/
override func touchesCancelled(touches: Set<UITouch>, withEvent event: UIEvent) {
self.reset()
}
/**
Touches Reset
*/
override func reset() {
currentCode = []
self.startingPoint = CGPointZero
super.reset()
}
}