|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * The examples provided by Facebook are for non-commercial testing and |
| 10 | + * evaluation purposes only. |
| 11 | + * |
| 12 | + * Facebook reserves all rights not expressly granted. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL |
| 17 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 18 | + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + * |
| 21 | + * @flow |
| 22 | + */ |
| 23 | +'use strict'; |
| 24 | + |
| 25 | +var React = require('react'); |
| 26 | +var ReactNative = require('react-native'); |
| 27 | +var { |
| 28 | + View, |
| 29 | + Text, |
| 30 | + Animated, |
| 31 | + StyleSheet, |
| 32 | + TouchableWithoutFeedback, |
| 33 | +} = ReactNative; |
| 34 | + |
| 35 | +class Tester extends React.Component { |
| 36 | + |
| 37 | + static title = 'Native Animated Problem'; |
| 38 | + static description = 'Test out Native Animations'; |
| 39 | + |
| 40 | + state = { |
| 41 | + opacity: new Animated.Value(0.25, {useNativeDriver: true}), |
| 42 | + showBlock2: false, |
| 43 | + }; |
| 44 | + |
| 45 | + visible = false; |
| 46 | + |
| 47 | + runAnimation = () => { |
| 48 | + this.visible = !this.visible; |
| 49 | + Animated.timing(this.state.opacity, { |
| 50 | + toValue: this.visible ? 1 : 0.25, |
| 51 | + duration: 500, |
| 52 | + useNativeDriver: true, |
| 53 | + }).start(); |
| 54 | + }; |
| 55 | + |
| 56 | + render() { |
| 57 | + return ( |
| 58 | + <View style={styles.container}> |
| 59 | + <View style={styles.section}> |
| 60 | + <TouchableWithoutFeedback onPress={this.runAnimation}> |
| 61 | + <View style={styles.button}> |
| 62 | + <Text>{'Run animation'}</Text> |
| 63 | + </View> |
| 64 | + </TouchableWithoutFeedback> |
| 65 | + </View> |
| 66 | + <View style={styles.section}> |
| 67 | + <TouchableWithoutFeedback onPress={() => { |
| 68 | + this.setState({ |
| 69 | + showBlock2: !this.state.showBlock2, |
| 70 | + }); |
| 71 | + }}> |
| 72 | + <View style={styles.button}> |
| 73 | + <Text>{'Toggle block 2'}</Text> |
| 74 | + </View> |
| 75 | + </TouchableWithoutFeedback> |
| 76 | + </View> |
| 77 | + <View style={styles.section}> |
| 78 | + {this._renderBlock(1)} |
| 79 | + </View> |
| 80 | + <View style={styles.section}> |
| 81 | + {this.state.showBlock2 && this._renderBlock(2)} |
| 82 | + </View> |
| 83 | + </View> |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + _renderBlock(key: number) { |
| 88 | + return ( |
| 89 | + <Animated.View |
| 90 | + key={key} |
| 91 | + style={[ |
| 92 | + styles.block, |
| 93 | + { |
| 94 | + opacity: this.state.opacity, |
| 95 | + } |
| 96 | + ]} |
| 97 | + /> |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +const styles = StyleSheet.create({ |
| 103 | + container: { |
| 104 | + padding: 10, |
| 105 | + }, |
| 106 | + button: { |
| 107 | + padding: 10, |
| 108 | + backgroundColor: 'green', |
| 109 | + }, |
| 110 | + section: { |
| 111 | + marginBottom: 20, |
| 112 | + }, |
| 113 | + block: { |
| 114 | + width: 50, |
| 115 | + height: 50, |
| 116 | + backgroundColor: 'blue', |
| 117 | + }, |
| 118 | +}); |
| 119 | + |
| 120 | +module.exports = Tester; |
0 commit comments