-
Notifications
You must be signed in to change notification settings - Fork 5
/
ResponderView.js
173 lines (147 loc) · 5.34 KB
/
ResponderView.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React from 'react'
import {
View,
StyleSheet
} from 'react-native'
let touchTimer;
class ResponderView extends React.PureComponent {
static defaultProps = {
handleSingleTouch : function(){},
handleDoubleTouch : function(){},
handleLeftAndRightMove: function(){},
handleLeftAndRightMoveComplete: function(){},
handleUpAndDownMoveInLeft: function(){},
handleUpAndDownMoveInRight : function(){}
};
constructor(props){
super(props);
this.state={
firstCoordinate:{x:0,y:0}, // for judging LeftAndRightMove
firstTimestamp:0, // for preventing misoperation
lastCoordinate:{x:0,y:0}, // for calculating move offset
moveEvent:null, // Responder Event
}
}
/********** Handle Event **********/
handleSingleTouch=(e)=>{
this.props.handleSingleTouch(e)
};
handleDoubleTouch=(e)=>{
this.props.handleDoubleTouch(e)
};
/**
* moveEventType: ['UpAndDownMoveInRight','UpAndDownMoveInLeft','LeftAndRightMove']
*
* @param offset: Number( currentCoordinate - lastCoordinate )
*
* **/
handleUpAndDownMoveInRight=(offset)=>{
this.props.handleUpAndDownMoveInRight(offset)
};
handleUpAndDownMoveInLeft=(offset)=>{
this.props.handleUpAndDownMoveInLeft(offset)
};
handleLeftAndRightMove=(offset)=>{
this.props.handleLeftAndRightMove(offset)
};
/********** Render **********/
render(){
return (
<View ref={'touchView'}
{...this.props}
style={[styles.touchView,this.props.style]}
onStartShouldSetResponder={this._onStartShouldSetResponder}
onResponderGrant={this._onResponderGrant}
onResponderMove={this._onResponderMove}
onResponderRelease={this._onResponderRelease}
>
{this.props.children}
</View>
)
}
/********** Life Cycle **********/
componentWillUnmount() {
clearTimeout(touchTimer)
}
/********** Gesture Responder System **********/
_onStartShouldSetResponder=(e)=>{
if(touchTimer){
this.handleDoubleTouch(e);
clearTimeout(touchTimer)
}
return true
};
_onResponderGrant=async (e)=>{
const {pageX,pageY,locationX,locationY,timestamp} = e.nativeEvent;
this.refs.touchView.measure(async (ox, oy, width, height, px, py) => {
await this.setState({
firstCoordinate:{x:locationX,y:locationY},
firstTimestamp:timestamp,
lastCoordinate:{x:pageX,y:pageY}
});
})
};
_onResponderMove=async (e)=>{
e.persist();
const {pageX,pageY,locationX,locationY,timestamp} = e.nativeEvent;
const {moveEvent,firstCoordinate,lastCoordinate,firstTimestamp} = this.state;
if(timestamp-firstTimestamp>300){
await this.setState({lastCoordinate:{x:pageX,y:pageY}});
this.refs.touchView.measure(async (ox, oy, width, height, px, py) => {
if(moveEvent){
if(moveEvent==='LeftAndRightMove'){
this.handleLeftAndRightMove(pageX-lastCoordinate.x)
}else if(moveEvent==='UpAndDownMoveInRight'){
this.handleUpAndDownMoveInRight(lastCoordinate.y-pageY)
}else if(moveEvent==='UpAndDownMoveInLeft'){
this.handleUpAndDownMoveInLeft(lastCoordinate.y-pageY)
}
}else {
if(Math.abs(firstCoordinate.x-locationX)> Math.abs(firstCoordinate.y-locationY)){
this.handleLeftAndRightMove(pageX-lastCoordinate.x);
await this.setState({
moveEvent:'LeftAndRightMove'
})
}else if(locationX>width/2){
this.handleUpAndDownMoveInRight(lastCoordinate.y-pageY);
await this.setState({
moveEvent:'UpAndDownMoveInRight'
})
}else if(locationX<width/2){
this.handleUpAndDownMoveInLeft(lastCoordinate.y-pageY);
await this.setState({
moveEvent:'UpAndDownMoveInLeft'
})
}
}
});
}
};
_onResponderRelease=(e)=>{
const {moveEvent} = this.state;
if(moveEvent){
moveEvent=='LeftAndRightMove'&&this.props.handleLeftAndRightMoveComplete();
this.setState({
moveEvent:null,
firstTimestamp:e.nativeEvent.timestamp
})
}else {
if(!touchTimer){
touchTimer = setTimeout(()=>{
this.handleSingleTouch(e);
touchTimer=undefined
},300)
}else {
touchTimer=undefined
}
}
}
}
const styles = StyleSheet.create({
touchView:{
flex:1,
backgroundColor: 'transparent',
opacity:1
}
});
export default ResponderView