My question is quite specific:
How do I chain two animations so I can move an item from X to Y and then from Y to Z?
I got a view that I want to animate from position (x,y) to (x+a, y+b) and then make it "hover" there. I thought the animation would continue from the point it left off but I was proven wrong... when it executes the loop it restarts from the initial value (0,0) instead of its last position.
// this is in my index.js
class App extends Component {
constructor(props) {
super(props);
this.translateValue = new Animated.ValueXY({x: 0, y: 0});
}
componentDidMount() {
Animated.sequence([
Animated.timing(this.translateValue,
{ toValue: { x: 30, y: 30 }, duration: 1000, easing: Easing.linear }),
Animated.loop(
Animated.sequence([
Animated.timing(this.translateValue,
{ toValue: { x: 30, y: 20 }, duration: 1000, easing: Easing.linear }),
Animated.timing(this.translateValue,
{ toValue: { x: 30, y: 30 }, duration: 1000, easing: Easing.linear })
]),
{ iterations: 1000 })
]).start();
}
render() {
const translateTransform = this.translateValue.getTranslateTransform();
return (
<View style={{ flex: 1 }}>
<Animated.View style={{
height: 30,
width: 30,
backgroundColor: "blue",
position: "absolute",
transform: translateTransform }} />
</View>
);
}
}
Do I need to call this.translateValue.setValue({x: 30, y: 30 })
once the first animation of the sequence has ended? if so, how?
Edit: I was looking for a declarative mechanism. Unfortunately, I think there is no declarative way of calling the setValue
as part of the animation composition.
Animated
has a callback for ending and you can chain two animations like this:
constructor() {
this.state = {
translation: 1,
}
this.fade = new Animated.Value(0)
}
fade_1(){
this.fade.setValue(0)
this.setState({translation: this.fade.interpolate({
inputRange: [0, 1],
outputRange: [ 1 , 0]
})})
Animated.timing(
this.fade,
{
toValue: 1,
duration: 3000,
useNativeDriver: true
}
).start(() => this.fade_2()) // we chain animation's here
}
fade_2(){
this.fade.setValue(0)
this.setState({translation: this.fade.interpolate({
inputRange: [0, 1],
outputRange: [ 0 , 1]
})})
Animated.timing(
this.fade,
{
toValue: 1,
duration: 3000,
useNativeDriver: true
}
).start()
}
render() {
return(
<View style={{backgroundColor:'#fff' , flex:1 ,alignItems: 'center' , justifyContent: 'center' }}>
<Animated.View style={{width: 150 , height: 150 , alignItems: 'center', opacity: this.state.translation }}>
<Image source={require('YOUR IMAGE URI')} style={{width: 150 , height: 150}}/>
</Animated.View>
<TouchableOpacity style={{flex: 1 , alignItems: 'center', justifyContent: 'center'}}
onPress={()=> this.fade_1()}
>
<Text> START </Text>
</TouchableOpacity>
</View>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With