Repeat animation with new animated api

后端 未结 8 872
时光取名叫无心
时光取名叫无心 2021-01-31 14:40

React-native introduce new Animated API, I want to make a loop animation such as a bubble scale up then scale down and repeat that progress.

However I can n

相关标签:
8条回答
  • 2021-01-31 15:10

    It seems that 'looping' is not supported by the Animated api for now.

    I managed to do that by start the animation again when it finished.

    startAnimation() {
      Animated.timing(this._animatedValue, {
        toValue: 100,
        duration: 1000,
      }).start(() => {
        this.startAnimation();
      });
    }
    

    Looking forward to a better solution...

    0 讨论(0)
  • 2021-01-31 15:14

    Try something like this:

    componentDidMount() {
        this.bootAnimation();
      }
    
      bootAnimation() {
        this.animation = Animated.loop(
          Animated.timing(this.state.progress, {
            toValue: 1,
            duration: 5000
          })
        ).start();
      }

    0 讨论(0)
  • 2021-01-31 15:20

    You can set another animation then call the animation again:

    An example I did to fade text in and out:

      textAnimate: function() {
        Animated.timing(
          this.state.textOpacity,
          {
            toValue: 0.3,                         
            duration: 500, 
          }
        ).start(() => {
          Animated.timing(  
            this.state.textOpacity,            
            {
              toValue: 1,                    
              duration: 500,          
            }
          ).start(() => {
              this.textAnimate();
            });
        });    
      },
    
      componentDidMount: function() {
        this.state.textOpacity.setValue(1)
        this.textAnimate();
      },
    
    0 讨论(0)
  • 2021-01-31 15:21

    Here's another example for an infinite animation using hooks and iterations set to "infinity". Avoids the use of the recursion in previous answers which sometimes led to funky behaviour during e2e testing for us.

      const rotation = React.useRef(new Animated.Value(0)).current;
    
      function runAnimation() {
        return Animated.loop(
          Animated.timing(rotation, {
            toValue: 1,
            duration: 1200,
            easing: Easing.linear,
            useNativeDriver: true,
          }),
          {resetBeforeIteration: true, iterations: Number.MAX_SAFE_INTEGER},
        );
      }
    
      React.useEffect(() => {
        const animation = runAnimation();
        return () => animation.stop();
      }, []);
    
    0 讨论(0)
  • 2021-01-31 15:24

    improved version of @bcomerford answer

    //this.state.animatedStartValue = 0;
    
    function cycleAnimation() {
      Animated.sequence([
        Animated.timing(this.state.animatedStartValue, {
          toValue: 1,
          duration: 500,
          delay: 1000
        }),
        Animated.timing(this.state.animatedStartValue, {
          toValue: 0,
          duration: 500
       })
      ]).start(event => {
        if (event.finished) {
          cycleAnimation();
        }
      });
    }
    
    0 讨论(0)
  • 2021-01-31 15:24

    Not sure if it's hacky, but I use this:

    Animated.spring(this.state.rotation, {
      toValue: 5,
      stiffness: 220, // the higher value, the faster the animation
      damping: 0.000001, // never stop wiggle wiggle wiggle
    }).start();
    

    It's creating spring animation that will never (technically, for a very very very long time) stop waving.

    For most of my cases it was enough. Also it has great performance as it does not require any JS tread action ever during animation.

    If eventually you'd like to stop it gracefully:

    Animated.spring(this.state.rotation, {
      toValue: 0,
      stiffness: 220, // the higher value, the faster the animation
      damping: 10, // never stop wiggle wiggle wiggle
    }).start();
    

    And it'll nicely 'slow down' until it stops.

    0 讨论(0)
提交回复
热议问题