forked from glepur/react-native-swipe-gestures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (140 loc) · 4.55 KB
/
index.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
157
158
159
"use strict";
import React, { Component } from "react";
import { View, PanResponder } from "react-native";
export const swipeDirections = {
SWIPE_UP: "SWIPE_UP",
SWIPE_DOWN: "SWIPE_DOWN",
SWIPE_LEFT: "SWIPE_LEFT",
SWIPE_RIGHT: "SWIPE_RIGHT"
};
const swipeConfig = {
velocityThreshold: 0.3,
directionalOffsetThreshold: 80,
gestureIsClickThreshold: 5,
needVerticalScroll: false,
scrollVerticalThreshold: 5
};
function isValidSwipe(
velocity,
velocityThreshold,
directionalOffset,
directionalOffsetThreshold
) {
return (
Math.abs(velocity) > velocityThreshold &&
Math.abs(directionalOffset) < directionalOffsetThreshold
);
}
class GestureRecognizer extends Component {
constructor(props, context) {
super(props, context);
this.swipeConfig = Object.assign(swipeConfig, props.config);
const responderEnd = this._handlePanResponderEnd.bind(this);
const shouldSetResponder = this._handleShouldSetPanResponder.bind(this);
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: shouldSetResponder,
onMoveShouldSetPanResponder: shouldSetResponder,
onPanResponderRelease: responderEnd,
onPanResponderTerminate: responderEnd
});
}
componentDidUpdate(prevProps) {
if (this.props.config !== prevProps.config) {
this.swipeConfig = Object.assign(swipeConfig, this.props.config);
}
}
_handleShouldSetPanResponder(evt, gestureState) {
// start --- 2020/12/4 fix the problem that scroll view can't work inside on ios
if (this.swipeConfig.needVerticalScroll
&& ( gestureState.dy > this.swipeConfig.scrollVerticalThreshold
|| gestureState.dy < -this.swipeConfig.scrollVerticalThreshold )
) {
return false
}
// end --- 2020/12/4 fix the problem that scroll view can't work inside on ios
return (
this.props.swipeEnabled &&
evt.nativeEvent.touches.length === 1 &&
!this._gestureIsClick(gestureState)
);
}
_gestureIsClick(gestureState) {
return (
Math.abs(gestureState.dx) < swipeConfig.gestureIsClickThreshold &&
Math.abs(gestureState.dy) < swipeConfig.gestureIsClickThreshold
);
}
_handlePanResponderEnd(evt, gestureState) {
const swipeDirection = this._getSwipeDirection(gestureState);
this._triggerSwipeHandlers(swipeDirection, gestureState);
}
_triggerSwipeHandlers(swipeDirection, gestureState) {
const {
onSwipe,
onSwipeUp,
onSwipeDown,
onSwipeLeft,
onSwipeRight
} = this.props;
const { SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN } = swipeDirections;
onSwipe && onSwipe(swipeDirection, gestureState);
switch (swipeDirection) {
case SWIPE_LEFT:
onSwipeLeft && onSwipeLeft(gestureState);
break;
case SWIPE_RIGHT:
onSwipeRight && onSwipeRight(gestureState);
break;
case SWIPE_UP:
onSwipeUp && onSwipeUp(gestureState);
break;
case SWIPE_DOWN:
onSwipeDown && onSwipeDown(gestureState);
break;
}
}
_getSwipeDirection(gestureState) {
const { SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN } = swipeDirections;
const { dx, dy } = gestureState;
const absDx = Math.abs(dx);
const absDy = Math.abs(dy);
const validHorizontal = this._isValidHorizontalSwipe(gestureState);
const validVertical = this._isValidVerticalSwipe(gestureState);
const horizontalDirection = dx > 0 ? SWIPE_RIGHT : SWIPE_LEFT;
const verticalDirection = dy > 0 ? SWIPE_DOWN : SWIPE_UP;
//check which delta is larger and choose that order to evaluate
if (absDx > absDy) {
if (validHorizontal) {
return horizontalDirection;
} else if (validVertical) {
return verticalDirection;
}
}
else {
if (validVertical) {
return verticalDirection;
}
else if (validHorizontal) {
return horizontalDirection;
}
}
return null;
}
_isValidHorizontalSwipe(gestureState) {
const { vx, dy } = gestureState;
const { velocityThreshold, directionalOffsetThreshold } = this.swipeConfig;
return isValidSwipe(vx, velocityThreshold, dy, directionalOffsetThreshold);
}
_isValidVerticalSwipe(gestureState) {
const { vy, dx } = gestureState;
const { velocityThreshold, directionalOffsetThreshold } = this.swipeConfig;
return isValidSwipe(vy, velocityThreshold, dx, directionalOffsetThreshold);
}
render() {
return <View {...this.props} {...this._panResponder.panHandlers} />;
}
}
GestureRecognizer.defaultProps = {
swipeEnabled: true,
};
export default GestureRecognizer;