React Native Lottie - 动画结束反转

问题描述 投票:0回答:2

Context

我是lottie-react-native的新手,并设法实现了我的第一个动画:

constructor(props) {
    super(props);
    this.state = {
        progress: new Animated.Value(0),
        loop: true
    }
}
componentDidMount() {
    this.animation.play();
}
render() {
const { progress, loop } = this.state;
return (
    <View style={{display:'flex',height:'auto', alignItems: 'center',justifyContent:'center'}}>
    <LottieView
    ref={animation => {
        this.animation = animation;
      }}
    speed={1}
    autoPlay
    source={NOACTIVITY}
    progress={progress}
    loop={loop}
    height={300}
    width={300}
    style={{margin:0,}}
  />
  </View>
)

}

The Problem

我现在正在尝试使用此动画创建一个循环,然后向前播放,然后再次启动该过程。

我做了一些研究并得出结论,这必须使用动画值和时间来完成?我发现很多例子(in the react native docs!)向前和向后但不在一起。

可以在组件上完成安装吗?或者它必须是一个单独的功能?

提前致谢!

reactjs loops native lottie
2个回答
1
投票

因此,您可以做的一件事是对this.state.progress使用Animated.loop(),然后在每次迭代时反转动画的速度。

AnimateFunction = () => {
  Animated.loop(
    Animated.timing(
      this.state.progress,
      {
        toValue: 1,
        duration: (your duration of anim),
        easing: Easing.linear()
      }
    )
  ).start(
    () => {
      this.animation.setSpeed(this.animation.speed * -1);
    }
  );
}

然后将componentDidMount更改为:

componentDidMount() {
  this.AnimateFunction();
}

我不确定没有测试但是你可能需要在每次循环后设置速度的同时重置this.state.progress.setValue(0)。如果它最初不起作用,请记住这一点。

虽然我有兴趣看看你是否和我有同样的问题。当我在React Native中循环lottie动画时出于某种原因,他们会在循环之间暂停...无论如何希望这对你有用


1
投票

我想出的解决方案是在循环中使用一个序列,如下所示:

AnimateFunction = () => {
    Animated.loop(
        Animated.sequence([
            Animated.timing(
                this.state.progress,
                {
                  toValue: 1,
                  duration: (5000),
                  //easing: Easing.linear()
                }
              ),
              Animated.timing(
                this.state.progress,
                {
                  toValue: 0,
                  duration: (5000),
                  //easing: Easing.linear()
                }
              )
        ])

    ).start();
  }

我发现添加缓动使动画在应用程序重新启动时跳转了一点,所以它现在被注释掉了。

© www.soinside.com 2019 - 2024. All rights reserved.