Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to chain multiple animations together in react native (declarative)?

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.

like image 655
rodrigoelp Avatar asked Jan 07 '18 09:01

rodrigoelp


1 Answers

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>
    )

} 
like image 53
amirhosein Avatar answered Sep 23 '22 18:09

amirhosein