forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollViewAnimatedExample.js
More file actions
107 lines (98 loc) · 2.72 KB
/
ScrollViewAnimatedExample.js
File metadata and controls
107 lines (98 loc) · 2.72 KB
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
/**
* 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 strict-local
*/
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {Component} = React;
const {
StyleSheet,
Text,
View,
Animated,
Easing,
TouchableOpacity,
Dimensions,
} = ReactNative;
class ScrollViewAnimatedExample extends Component<{...}> {
_scrollViewPos = new Animated.Value(0);
startAnimation: () => void = () => {
this._scrollViewPos.setValue(0);
Animated.timing(this._scrollViewPos, {
toValue: 100,
duration: 10000,
easing: Easing.linear,
useNativeDriver: true,
}).start();
};
render(): React.Node {
const interpolated = this._scrollViewPos.interpolate({
inputRange: [0, 1],
/* $FlowFixMe(>=0.38.0) - Flow error detected during the deployment of
* v0.38.0. To see the error, remove this comment and run flow */
outputRange: [0, 0.1],
});
const interpolated2 = this._scrollViewPos.interpolate({
inputRange: [0, 1],
/* $FlowFixMe(>=0.38.0) - Flow error detected during the deployment of
* v0.38.0. To see the error, remove this comment and run flow */
outputRange: ['0deg', '1deg'],
});
return (
<View style={styles.container}>
<Animated.View
style={{
width: 100,
height: 100,
backgroundColor: 'black',
transform: [{translateX: interpolated}, {rotate: interpolated2}],
}}
/>
<Animated.ScrollView
horizontal
scrollEventThrottle={16}
onScroll={Animated.event(
[{nativeEvent: {contentOffset: {x: this._scrollViewPos}}}],
{useNativeDriver: true},
)}>
<TouchableOpacity onPress={this.startAnimation}>
<View style={styles.button}>
<Text>Scroll me horizontally</Text>
</View>
</TouchableOpacity>
</Animated.ScrollView>
</View>
);
}
}
const {width, height} = Dimensions.get('window');
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
margin: 50,
width: width,
marginRight: width,
height: height / 2,
},
});
exports.title = '<ScrollViewAnimated>';
exports.description = 'Component that is animated when ScrollView is offset.';
exports.examples = [
{
title: 'Animated by scroll view',
render: function(): React.Element<typeof ScrollViewAnimatedExample> {
return <ScrollViewAnimatedExample />;
},
},
];