|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const React = require('react'); |
| 14 | +const ReactNative = require('react-native'); |
| 15 | +const {Component} = React; |
| 16 | +const { |
| 17 | + StyleSheet, |
| 18 | + Text, |
| 19 | + View, |
| 20 | + Animated, |
| 21 | + Easing, |
| 22 | + TouchableOpacity, |
| 23 | + Dimensions, |
| 24 | +} = ReactNative; |
| 25 | + |
| 26 | +class Button extends Component { |
| 27 | + render() { |
| 28 | + const {onPress, title} = this.props; |
| 29 | + return ( |
| 30 | + <TouchableOpacity onPress={onPress}> |
| 31 | + <View style={styles.button}> |
| 32 | + <Text>{title}</Text> |
| 33 | + </View> |
| 34 | + </TouchableOpacity> |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class ScrollViewAnimatedExample extends Component { |
| 40 | + _scrollViewPos = new Animated.Value(0); |
| 41 | + |
| 42 | + startAnimation = () => { |
| 43 | + this._scrollViewPos.setValue(0); |
| 44 | + Animated.timing(this._scrollViewPos, { |
| 45 | + toValue: 100, |
| 46 | + duration: 10000, |
| 47 | + easing: Easing.linear, |
| 48 | + useNativeDriver: true, |
| 49 | + }).start(); |
| 50 | + }; |
| 51 | + |
| 52 | + render() { |
| 53 | + const interpolated = this._scrollViewPos.interpolate({ |
| 54 | + inputRange: [0, 1], |
| 55 | + outputRange: [0, 0.1], |
| 56 | + }); |
| 57 | + const interpolated2 = this._scrollViewPos.interpolate({ |
| 58 | + inputRange: [0, 1], |
| 59 | + outputRange: ['0deg', '1deg'], |
| 60 | + }); |
| 61 | + return ( |
| 62 | + <View style={styles.container}> |
| 63 | + <Animated.View |
| 64 | + style={{ |
| 65 | + width: 100, |
| 66 | + height: 100, |
| 67 | + backgroundColor: 'black', |
| 68 | + transform: [{translateX: interpolated}, {rotate: interpolated2}], |
| 69 | + }} |
| 70 | + /> |
| 71 | + <Animated.ScrollView |
| 72 | + horizontal |
| 73 | + scrollEventThrottle={16} |
| 74 | + onScroll={Animated.event( |
| 75 | + [{nativeEvent: {contentOffset: {x: this._scrollViewPos}}}], |
| 76 | + {useNativeDriver: true}, |
| 77 | + )}> |
| 78 | + <Button |
| 79 | + onPress={this.startAnimation} |
| 80 | + title="Scroll me horizontally" |
| 81 | + /> |
| 82 | + </Animated.ScrollView> |
| 83 | + </View> |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const {width, height} = Dimensions.get('window'); |
| 89 | + |
| 90 | +const styles = StyleSheet.create({ |
| 91 | + container: { |
| 92 | + flex: 1, |
| 93 | + justifyContent: 'center', |
| 94 | + alignItems: 'center', |
| 95 | + backgroundColor: '#F5FCFF', |
| 96 | + }, |
| 97 | + button: { |
| 98 | + margin: 50, |
| 99 | + width: width, |
| 100 | + marginRight: width, |
| 101 | + height: height / 2, |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +exports.title = '<ScrollViewAnimated>'; |
| 106 | +exports.description = 'Component that is animated when ScrollView is offset.'; |
| 107 | + |
| 108 | +exports.examples = [ |
| 109 | + { |
| 110 | + title: 'Animated by scroll view', |
| 111 | + render: function(): React.Element<typeof ScrollViewAnimatedExample> { |
| 112 | + return <ScrollViewAnimatedExample />; |
| 113 | + }, |
| 114 | + }, |
| 115 | +]; |
0 commit comments