-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimpleGesture.js
156 lines (123 loc) · 2.96 KB
/
SimpleGesture.js
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
'use strict';
import { Dimensions } from 'react-native';
const FAST = 1;
const LONG = 0.5;
const MOVING = 0.2;
class SimpleGesture {
constructor(e, gs) {
let relativeGestureDistance = {
x: gs.dx / Dimensions.get('window').width,
y: gs.dy / Dimensions.get('window').height
};
Object.assign(this, gs, { relativeGestureDistance });
}
isVertical() {
// Is the vertical offset higher than the horizontal offset?
return !!(Math.abs(this.dy) > Math.abs(this.dx));
}
isHorizontal() {
// Is the horizontal offset higher than the vertical offset?
return !!!(this.isVertical());
}
// Swipe Up Gestures
isSimpleSwipeUp() {
// IF:
// (
// The user swiped up a significant percent of the screen height
// OR
// Swiped up in a quick motion
// )
// AND
// Didn't change their mind and start swiping down at the end
// THEN:
// Sure, let's call that a swipe up.
return !!(
(this.isLongSwipeUp() || this.isFastSwipeUp())
&& !this.isMovingDown()
);
}
isSwipeUp() {
if(!this.isVertical()) return false;
return !!(this.dy < 0);
}
isLongSwipeUp() {
return !!(this.relativeGestureDistance.y < -LONG);
}
isFastSwipeUp() {
return !!(this.vy < -FAST);
}
isMovingUp() {
return !!(this.vy < -MOVING);
}
// Swipe Down Gestures
isSimpleSwipeDown() {
// IF:
// (
// The user swiped down a significant percent of the screen height
// OR
// Swiped down in a quick motion
// )
// AND
// Didn't change their mind and start swiping up at the end
// THEN:
// Sure, let's call that a swipe down.
return !!(
(this.isLongSwipeDown() || this.isFastSwipeDown())
&& !this.isMovingUp()
);
}
isSwipeDown() {
if(!this.isVertical()) return false;
return !!(this.dy > 0);
}
isLongSwipeDown() {
return !!(this.relativeGestureDistance.y > LONG);
}
isFastSwipeDown() {
return !!(this.vy > FAST);
}
isMovingDown() {
return !!(this.vy > MOVING);
}
// Swipe Left Gestures
isSimpleSwipeLeft() {
return !!(
(this.isLongSwipeLeft() || this.isFastSwipeLeft())
&& !this.isMovingRight()
);
}
isSwipeLeft() {
if(!this.isHorizontal()) return false;
return !!(this.dx < 0);
}
isLongSwipeLeft() {
return !!(this.relativeGestureDistance.x < -LONG);
}
isFastSwipeLeft() {
return !!(this.vx < -FAST);
}
isMovingLeft() {
return !!(this.vx < -MOVING);
}
// Swipe Right Gestures
isSimpleSwipeRight() {
return !!(
(this.isLongSwipeRight() || this.isFastSwipeRight())
&& !this.isMovingLeft()
);
}
isSwipeRight() {
if(!this.isHorizontal()) return false;
return !!(this.dx > 0);
}
isLongSwipeRight() {
return !!(this.relativeGestureDistance.x > LONG);
}
isFastSwipeRight() {
return !!(this.vx > FAST);
}
isMovingRight() {
return !!(this.vx > MOVING);
}
}
export default SimpleGesture;