如何将多个动画链接在一起反应原生(声明)?

时间:2018-01-07 09:33:19

标签: javascript animation react-native

我的问题非常具体:

如何链接两个动画以便我可以将项目从X移动到Y然后从Y移动到Z?

我得到一个视图,我想从位置(x,y)动画到(x + a,y + b),然后使它成为" hover"那里。我认为动画将从它停止的点继续但我被证明是错误的......当它执行循环时,它从初始值(0,0)而不是它的最后位置重新开始。

// 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>
    );
  }
}

一旦序列的第一个动画结束,我是否需要调用this.translateValue.setValue({x: 30, y: 30 })?如果是这样,怎么样?

编辑:我一直在寻找声明机制。不幸的是,我认为没有声明方式来调用setValue作为动画组合的一部分。

2 个答案:

答案 0 :(得分:0)

Animated具有结尾的回调,您可以像这样链接两个动画:

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

} 

答案 1 :(得分:0)

我已经为AnimatedManager-https://github.com/pie6k/react-native-animated-manager

这样的场景创建了库

它允许您使用诸如承诺之类的动画,例如

class Foo extends Component {
  // ....
  private animated = {
    size: new AnimatedManager(20),
    rotation: new AnimatedManager(0),
    positionY: new AnimatedManager(20),
  }
  async animate() {
    const { animated } = this;
    await animated.size.spring({toValue: 50});
    await Promise.all([
      animated.rotation.timing({toValue: 50}),
      animated.size.spring({toValue: 15}),
    ]);
    await animated.positionY.spring({toValue: 100});
  }

}