forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathAnimatedExample.js
304 lines (298 loc) · 8.73 KB
/
AnimatedExample.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const React = require('react');
const {Animated, Easing, StyleSheet, Text, View} = require('react-native');
const RNTesterButton = require('./RNTesterButton');
const styles = StyleSheet.create({
content: {
backgroundColor: 'deepskyblue',
borderWidth: 1,
borderColor: 'dodgerblue',
padding: 20,
margin: 20,
borderRadius: 10,
alignItems: 'center',
},
rotatingImage: {
width: 70,
height: 70,
},
});
exports.framework = 'React';
exports.title = 'Animated - Examples';
exports.description =
'Animated provides a powerful ' +
'and easy-to-use API for building modern, ' +
'interactive user experiences.';
exports.examples = [
{
title: 'FadeInView',
description:
'Uses a simple timing animation to ' +
'bring opacity from 0 to 1 when the component ' +
'mounts.',
render: function() {
class FadeInView extends React.Component<$FlowFixMeProps, any> {
constructor(props) {
super(props);
this.state = {
fadeAnim: new Animated.Value(0), // opacity 0
};
}
componentDidMount() {
Animated.timing(
// Uses easing functions
this.state.fadeAnim, // The value to drive
{
toValue: 1, // Target
duration: 2000, // Configuration
},
).start(); // Don't forget start!
}
render() {
return (
<Animated.View // Special animatable View
style={{
opacity: this.state.fadeAnim, // Binds
}}>
{this.props.children}
</Animated.View>
);
}
}
type Props = $ReadOnly<{||}>;
type State = {|show: boolean|};
class FadeInExample extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
show: true,
};
}
render() {
return (
<View>
<RNTesterButton
onPress={() => {
this.setState(state => ({show: !state.show}));
}}>
Press to {this.state.show ? 'Hide' : 'Show'}
</RNTesterButton>
{this.state.show && (
<FadeInView>
<View style={styles.content}>
<Text>FadeInView</Text>
</View>
</FadeInView>
)}
</View>
);
}
}
return <FadeInExample />;
},
},
{
title: 'Transform Bounce',
description:
'One `Animated.Value` is driven by a ' +
'spring with custom constants and mapped to an ' +
'ordered set of transforms. Each transform has ' +
'an interpolation to convert the value into the ' +
'right range and units.',
render: function() {
this.anim = this.anim || new Animated.Value(0);
return (
<View>
<RNTesterButton
onPress={() => {
Animated.spring(this.anim, {
toValue: 0, // Returns to the start
velocity: 3, // Velocity makes it move
tension: -10, // Slow
friction: 1, // Oscillate a lot
}).start();
}}>
Press to Fling it!
</RNTesterButton>
<Animated.View
style={[
styles.content,
{
transform: [
// Array order matters
{
scale: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [1, 4],
}),
},
{
translateX: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [0, 500],
}),
},
{
rotate: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [
'0deg',
'360deg', // 'deg' or 'rad'
],
}),
},
],
},
]}>
<Text>Transforms!</Text>
</Animated.View>
</View>
);
},
},
{
title: 'Composite Animations with Easing',
description:
'Sequence, parallel, delay, and ' +
'stagger with different easing functions.',
render: function() {
this.anims = this.anims || [1, 2, 3].map(() => new Animated.Value(0));
return (
<View>
<RNTesterButton
onPress={() => {
const timing = Animated.timing;
Animated.sequence([
// One after the other
timing(this.anims[0], {
toValue: 200,
easing: Easing.linear,
}),
Animated.delay(400), // Use with sequence
timing(this.anims[0], {
toValue: 0,
easing: Easing.elastic(2), // Springy
}),
Animated.delay(400),
Animated.stagger(
200,
this.anims
.map(anim => timing(anim, {toValue: 200}))
.concat(this.anims.map(anim => timing(anim, {toValue: 0}))),
),
Animated.delay(400),
Animated.parallel(
[
Easing.inOut(Easing.quad), // Symmetric
Easing.back(1.5), // Goes backwards first
Easing.ease, // Default bezier
].map((easing, ii) =>
timing(this.anims[ii], {
toValue: 320,
easing,
duration: 3000,
}),
),
),
Animated.delay(400),
Animated.stagger(
200,
this.anims.map(anim =>
timing(anim, {
toValue: 0,
easing: Easing.bounce, // Like a ball
duration: 2000,
}),
),
),
]).start();
}}>
Press to Animate
</RNTesterButton>
{['Composite', 'Easing', 'Animations!'].map((text, ii) => (
<Animated.View
key={text}
style={[
styles.content,
{
left: this.anims[ii],
},
]}>
<Text>{text}</Text>
</Animated.View>
))}
</View>
);
},
},
{
title: 'Rotating Images',
description: 'Simple Animated.Image rotation.',
render: function() {
this.anim = this.anim || new Animated.Value(0);
return (
<View>
<RNTesterButton
onPress={() => {
Animated.spring(this.anim, {
toValue: 0, // Returns to the start
velocity: 3, // Velocity makes it move
tension: -10, // Slow
friction: 1, // Oscillate a lot
}).start();
}}>
Press to Spin it!
</RNTesterButton>
<Animated.Image
source={require('./bunny.png')}
style={[
styles.rotatingImage,
{
transform: [
{
scale: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [1, 10],
}),
},
{
translateX: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [0, 100],
}),
},
{
rotate: this.anim.interpolate({
inputRange: [0, 1],
outputRange: [
'0deg',
'360deg', // 'deg' or 'rad'
],
}),
},
],
},
]}
/>
</View>
);
},
},
{
title: 'Continuous Interactions',
description:
'Gesture events, chaining, 2D ' +
'values, interrupting and transitioning ' +
'animations, etc.',
render: () => <Text>Checkout the Gratuitous Animation App!</Text>,
},
];