简体   繁体   English

React Native Lottie - 在 Animation 结束反向

[英]React Native Lottie - Upon Animation End Reverse

Context语境

I am new to lottie-react-native and have managed to implement my first animation:我是 lottie-react-native 的新手,已经成功实现了我的第一个 animation:

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问题

I am now trying to create a loop with this animation that plays it forwards, then plays it backwards and then starts the process again.我现在正在尝试用这个 animation 创建一个循环,向前播放,然后向后播放,然后再次开始这个过程。

I have done some research and concluded that this must be completed using the animated values and timing?我做了一些研究并得出结论,这必须使用动画值和时间来完成? I have found many examples ( in the react native docs! ) of playing forwards and backwards but not together.我发现了许多向前和向后播放但不是一起播放的示例(在 react native 文档中! )。

Can this be completed on component did mount?这可以在已安装的组件上完成吗? or does it have to be a separate function?还是必须是单独的 function?

Thanks in advance!提前致谢!

The solution I came up with was using a sequence inside a loop as follows:我想出的解决方案是在循环中使用一个序列,如下所示:

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

I found that adding easing made the animation jump a little when the application restarted at 0 so it is commented out for now.我发现当应用程序从 0 重新启动时,添加缓动会使动画跳跃一点,因此现在将其注释掉。

So one thing you can do is use Animated.loop() for this.state.progress and then reverse the speed of the animation every iteration.因此,您可以做的一件事是将 Animated.loop() 用于 this.state.progress,然后在每次迭代时反转动画的速度。

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

and then change your componentDidMount to:然后将您的 componentDidMount 更改为:

componentDidMount() {
  this.AnimateFunction();
}

I am not sure without testing but it is possible you may need to reset this.state.progress.setValue(0) at the same time you set speed after each loop.我不确定没有测试,但您可能需要在每次循环后设置速度的同时重置 this.state.progress.setValue(0) 。 Keep that in mind in case it doesnt work at first.请记住这一点,以防它起初不起作用。

Although I am interested to see if you have the same following issue as I do.虽然我很想知道您是否和我有相同的以下问题。 When I loop lottie animations in React Native for some reason they pause in between loops... anyways hope this works for you当我出于某种原因在 React Native 中循环 lottie 动画时,它们会在循环之间暂停......无论如何希望这对你有用

onAnimationFinish you can use when animatation is finished onAnimationFinish 你可以在动画完成时使用

<LottieView
          ref={(el) => {
            lottie = el;
          }}
          autoPlay={false}
          loop={false}
          style={styles.lottie}
          source={animationFile}
          enableMergePathsAndroidForKitKatAndAbove
          onAnimationFinish={onAnimationFinish}
        />

Better and clean solution更好更干净的解决方案

const defaultEvent = {
    eventName: 'complete',
    callback: () => {
        alert("loopComplete")
       /*Insert your handlers here*/
    },
};

And then on your Lottie component:然后在你的 Lottie 组件上:

<Lottie
    options={defaultOptions}
    height={400}
    width={400}
    eventListeners={[defaultEvent]}
/>

Event names you can use: complete , loopComplete , enterFrame , segmentStart , config_ready , data_ready , loaded_images , DOMLoaded , destroy事件名称可以使用: completeloopCompleteenterFramesegmentStartconfig_readydata_readyloaded_imagesDOMLoadeddestroy

(Edit) I read later that this is for react-native, unforturnately, and I apologize, this is a react-js solution (编辑)我后来读到这是针对本机的,不幸的是,我很抱歉,这是一个 react-js 解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM