-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
145 lines (134 loc) · 4.07 KB
/
App.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
import React, { Component } from 'react';
import {
Animated,
Button,
StyleSheet,
Easing,
View,
PanResponder,
} from 'react-native';
import jake from './jake.png';
export default class App extends Component {
constructor(props) {
super(props);
this._rotationAnimation = new Animated.Value(0);
this._rotationOffset = 0;
this._gestureOffset = { x: 0, y: 0 };
this._gestureValue = new Animated.ValueXY();
this._panResponder = PanResponder.create({
// Ask to be the responder:
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
// The gesture has started. Show visual feedback so the user knows
// what is happening!
// gestureState.d{x,y} will be set to zero now
},
onPanResponderMove: (evt, gestureState) => {
// The most recent move distance is gestureState.move{X,Y}
// The accumulated gesture distance since becoming responder is
// gestureState.d{x,y}
this._gestureValue.setValue({
x: this._gestureOffset.x + gestureState.dx,
y: this._gestureOffset.y + gestureState.dy
});
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
// The user has released all touches while this view is the
// responder. This typically means a gesture has succeeded
this._gestureOffset.x += gestureState.dx;
this._gestureOffset.y += gestureState.dy;
},
onPanResponderTerminate: (evt, gestureState) => {
// Another component has become the responder, so this gesture
// should be cancelled
},
onShouldBlockNativeResponder: (evt, gestureState) => {
// Returns whether this component should block native components from becoming the JS
// responder. Returns true by default. Is currently only supported on android.
return true;
},
});
this.state = {
spinning: false,
};
}
startLoopAnimation = () => {
this._rotationAnimation.setOffset(this._rotationOffset);
Animated.loop(
Animated.timing(this._rotationAnimation, {
toValue: 1,
duration: 2000,
easing: Easing.linear,
})
).start();
};
stopLoopAnimation = () => {
this._rotationAnimation.stopAnimation(currentValue => {
this._rotationOffset = currentValue;
});
};
toggleSpinning = () => {
this.setState({ spinning: !this.state.spinning }, () => {
this.state.spinning
? this.startLoopAnimation()
: this.stopLoopAnimation();
});
};
getRotationAnimation = () => {
const rotate = this._rotationAnimation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
return { rotate };
};
render() {
return (
<View style={[styles.container]}>
<Animated.Image
source={jake}
style={{
transform: [
this.getRotationAnimation(),
{ translateX: this._gestureValue.x },
{ translateY: this._gestureValue.y },
],
}}
{...this._panResponder.panHandlers}
/>
<View style={styles.buttonsContainer}>
<Button
style={styles.button}
title={
this.state.spinning ? 'Turn Spinning Off' : 'Turn Spinning On'
}
onPress={() => this.toggleSpinning()}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
paddingTop: 64,
paddingBottom: 32,
},
buttonsContainer: {
flexDirection: 'row',
justifyContent: 'space-evenly',
position: 'absolute',
bottom: 16,
width: '100%',
},
button: {
width: 100,
},
});