Created
May 16, 2016 13:09
-
-
Save imaimiami/dc90f6315c2964228de8bf3dd3722b3c to your computer and use it in GitHub Desktop.
React Native Animated Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { | |
AppRegistry, | |
StyleSheet, | |
Text, | |
View, | |
Animated, | |
TouchableWithoutFeedback, | |
LayoutAnimation | |
} from 'react-native'; | |
export default class ReactNativeAnimatedSample extends Component { | |
constructor(props: any) { | |
super(props); | |
this.state = { | |
greenWidth: 100, | |
whiteDegree: new Animated.Value(0), | |
yellowScale: new Animated.Value(1), | |
backgroundColor: new Animated.Value(0) | |
}; | |
} | |
componentWillMount() { | |
} | |
componentDidMount() { | |
this.state.yellowScale.addListener((value) => this._yellowScale = value.value); | |
this.state.yellowScale.setValue(1); | |
this._changeBackgroundColor(); | |
} | |
_changeBackgroundColor() { | |
this.state.backgroundColor.setValue(0); | |
this.state.whiteDegree.setValue(0); | |
Animated.sequence([ | |
Animated.parallel([ | |
Animated.timing( | |
this.state.backgroundColor, | |
{toValue: 300, duration: 2000} | |
), | |
Animated.spring( | |
this.state.whiteDegree, | |
{toValue: 1, duration: 1500} | |
) | |
]), | |
Animated.parallel([ | |
Animated.timing( | |
this.state.backgroundColor, | |
{toValue: 0, duration: 2000} | |
), | |
Animated.spring( | |
this.state.whiteDegree, | |
{toValue: 0, duration: 1500} | |
) | |
]) | |
]).start(this._changeBackgroundColor.bind(this)); | |
} | |
_onPressGreen() { | |
LayoutAnimation.spring(); | |
this.setState({greenWidth: this.state.greenWidth + 20}); | |
} | |
_onPressYellow() { | |
Animated.spring( | |
this.state.yellowScale, | |
{ | |
toValue: this._yellowScale - 0.1, | |
friction: 1, | |
} | |
).start(); | |
} | |
render() { | |
var color = this.state.backgroundColor.interpolate({ | |
inputRange: [0, 150, 300], | |
outputRange: ['rgba(1, 87, 155, 1.0)', 'rgba(26, 35, 255, 1.0)', 'rgba(38, 50, 56, 1.0)'] | |
}); | |
var deg = this.state.whiteDegree.interpolate({ | |
inputRange: [0, 1], | |
outputRange: ['0deg', '180deg'] | |
}); | |
return ( | |
<Animated.View style={[styles.container, {backgroundColor: color}]}> | |
<Animated.View style={{backgroundColor: "ghostwhite", shadowColor: "lightgray", borderRadius: 6, width: 100, height: 100, margin: 20, transform: [{rotate: deg}]}}/> | |
<TouchableWithoutFeedback onPress={this._onPressGreen.bind(this)}> | |
<View style={{backgroundColor: "green", width: this.state.greenWidth, height: 100, margin: 20}}/> | |
</TouchableWithoutFeedback> | |
<TouchableWithoutFeedback onPress={this._onPressYellow.bind(this)}> | |
<Animated.View | |
style={{ | |
backgroundColor: "yellow", | |
width: 100, | |
height: 100, | |
transform: [ | |
{scale: this.state.yellowScale}, | |
] | |
}} | |
/> | |
</TouchableWithoutFeedback> | |
</Animated.View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
alignItems: 'center' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment