-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanDemo.js
executable file
·169 lines (149 loc) · 4.12 KB
/
PanDemo.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
// Adapted from https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/PanResponderExample.js
'use strict';
import React, { Component } from 'react';
import {
StyleSheet,
PanResponder,
View,
Text
} from 'react-native';
const CIRCLE_SIZE = 40;
const CIRCLE_COLOR = 'blue';
const CIRCLE_HIGHLIGHT_COLOR = 'green';
class PanResponderExample extends Component {
// Set some initial values.
_panResponder = {}
_previousLeft = 0
_previousTop = 0
_circleStyles = {}
circle = null
constructor(props) {
super(props);
this.state = {
numberActiveTouches: 0,
moveX: 0,
moveY: 0,
x0: 0,
y0: 0,
dx: 0,
dy: 0,
vx: 0,
vy: 0
};
}
componentWillMount() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
onPanResponderGrant: this._handlePanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminate: this._handlePanResponderEnd,
});
this._previousLeft = 20;
this._previousTop = 84;
this._circleStyles = {
style: {
left: this._previousLeft,
top: this._previousTop,
}
};
}
componentDidMount() {
this._updatePosition();
}
render() {
return (
<View style={styles.container}>
<View
ref={(circle) => {
this.circle = circle;
}}
style={styles.circle}
{...this._panResponder.panHandlers}/>
<Text>
{this.state.numberActiveTouches} touches,
dx: {this.state.dx},
dy: {this.state.dy},
vx: {this.state.vx},
vy: {this.state.vy}
</Text>
</View>
);
}
// _highlight and _unHighlight get called by PanResponder methods,
// providing visual feedback to the user.
_highlight = () => {
this.circle && this.circle.setNativeProps({
style: {
backgroundColor: CIRCLE_HIGHLIGHT_COLOR
}
});
}
_unHighlight = () => {
this.circle && this.circle.setNativeProps({
style: {
backgroundColor: CIRCLE_COLOR
}
});
}
// We're controlling the circle's position directly with setNativeProps.
_updatePosition = () => {
this.circle && this.circle.setNativeProps(this._circleStyles);
}
//noinspection JSAnnotator
_handleStartShouldSetPanResponder = (e: Object, gestureState: Object) => {
// Should we become active when the user presses down on the circle?
return true;
}
//noinspection JSAnnotator
_handleMoveShouldSetPanResponder = (e: Object, gestureState: Object) => {
// Should we become active when the user moves a touch over the circle?
return true;
}
//noinspection JSAnnotator
_handlePanResponderGrant = (e: Object, gestureState: Object) => {
this._highlight();
}
//noinspection JSAnnotator
_handlePanResponderMove = (e: å, gestureState: Object) => {
this.setState({
stateID: gestureState.stateID,
moveX: gestureState.moveX,
moveY: gestureState.moveY,
x0: gestureState.x0,
y0: gestureState.y0,
dx: gestureState.dx,
dy: gestureState.dy,
vx: gestureState.vx,
vy: gestureState.vy,
numberActiveTouches: gestureState.numberActiveTouches
});
// Calculate current position using deltas
this._circleStyles.style.left = this._previousLeft + gestureState.dx;
this._circleStyles.style.top = this._previousTop + gestureState.dy;
this._updatePosition();
}
//noinspection JSAnnotator
_handlePanResponderEnd = (e: Object, gestureState: Object) => {
this._unHighlight();
this._previousLeft += gestureState.dx;
this._previousTop += gestureState.dy;
}
}
const styles = StyleSheet.create({
circle: {
width: CIRCLE_SIZE,
height: CIRCLE_SIZE,
borderRadius: CIRCLE_SIZE / 2,
backgroundColor: CIRCLE_COLOR,
position: 'absolute',
left: 0,
top: 0,
},
container: {
flex: 1,
paddingTop: 64,
},
});
export default PanResponderExample;