forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlingGestureHandler.js
More file actions
137 lines (124 loc) · 3.38 KB
/
FlingGestureHandler.js
File metadata and controls
137 lines (124 loc) · 3.38 KB
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
import Hammer from 'hammerjs';
import { Direction } from './constants';
import { GesturePropError } from './Errors';
import DraggingGestureHandler from './DraggingGestureHandler';
import { isnan } from './utils';
class FlingGestureHandler extends DraggingGestureHandler {
get name() {
return 'swipe';
}
get NativeGestureClass() {
return Hammer.Swipe;
}
onGestureActivated(event) {
this.sendEvent({
...event,
eventType: Hammer.INPUT_MOVE,
isFinal: false,
isFirst: true,
});
this.isGestureRunning = false;
this.hasGestureFailed = false;
this.sendEvent({
...event,
eventType: Hammer.INPUT_END,
isFinal: true,
});
}
onRawEvent(ev) {
super.onRawEvent(ev);
if (this.hasGestureFailed) {
return;
}
// Hammer doesn't send a `cancel` event for taps.
// Manually fail the event.
if (ev.isFinal) {
setTimeout(() => {
if (this.isGestureRunning) {
this.cancelEvent(ev);
}
});
} else if (!this.hasGestureFailed && !this.isGestureRunning) {
// Tap Gesture start event
const gesture = this.hammer.get(this.name);
if (gesture.options.enable(gesture, ev)) {
this.onStart(ev);
this.sendEvent(ev);
}
}
}
getHammerConfig() {
return {
pointers: this.config.numberOfPointers,
direction: this.getDirection(),
};
}
getTargetDirections(direction) {
const directions = [];
if (direction & Direction.RIGHT) {
directions.push(Hammer.DIRECTION_RIGHT);
}
if (direction & Direction.LEFT) {
directions.push(Hammer.DIRECTION_LEFT);
}
if (direction & Direction.UP) {
directions.push(Hammer.DIRECTION_UP);
}
if (direction & Direction.DOWN) {
directions.push(Hammer.DIRECTION_DOWN);
}
// const hammerDirection = directions.reduce((a, b) => a | b, 0);
return directions;
}
getDirection() {
const { direction } = this.getConfig();
let directions = [];
if (direction & Direction.RIGHT) {
directions.push(Hammer.DIRECTION_HORIZONTAL);
}
if (direction & Direction.LEFT) {
directions.push(Hammer.DIRECTION_HORIZONTAL);
}
if (direction & Direction.UP) {
directions.push(Hammer.DIRECTION_VERTICAL);
}
if (direction & Direction.DOWN) {
directions.push(Hammer.DIRECTION_VERTICAL);
}
directions = [...new Set(directions)];
if (directions.length === 0) return Hammer.DIRECTION_NONE;
if (directions.length === 1) return directions[0];
return Hammer.DIRECTION_ALL;
}
isGestureEnabledForEvent(
{
minPointers,
maxPointers,
numberOfPointers,
maxDist,
maxDeltaX,
maxDeltaY,
maxDistSq,
shouldCancelWhenOutside,
},
recognizer,
{ maxPointers: pointerLength, deltaX: dx, deltaY: dy, ...props }
) {
const validPointerCount = pointerLength === numberOfPointers;
if (!validPointerCount && this.isGestureRunning) {
return { failed: true };
}
return { success: validPointerCount };
}
updateGestureConfig({ numberOfPointers = 1, direction, ...props }) {
if (isnan(direction) || typeof direction !== 'number') {
throw new GesturePropError('direction', direction, 'number');
}
return super.updateGestureConfig({
numberOfPointers,
direction,
...props,
});
}
}
export default FlingGestureHandler;