Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Animated Loop start callback

Tags:

react-native

I am struggling with React Native Animations here. The outcome is simple, I have an Animated.Image which I want to spin.
All good, until I want to loop thru the animation for n times and do something when it stops.
I have the following code .

Animated.loop(
  Animated.timing(this.state.spin, {
    toValue: 360,
    duration: 1000,
    easing: Easing.linear,
    useNativeDriver: true,
  }), {
    iterations: 3
  }
).start(() => {
  console.log('done');
});

It spins 3 times as per loop iteration but no callback was fired when animation ends.

Here the Expo which replicates this: https://snack.expo.io/S1PjnfB9-

like image 273
BogdanD Avatar asked Sep 15 '25 11:09

BogdanD


1 Answers

Try the code below

Animated.loop(
  Animated.timing(this.state.spin, {
    toValue: 360,
    duration: 1000,
    easing: Easing.linear,
    useNativeDriver: true,
  }), {
    iterations: 3
  }
).start(event => {
    if (event.finished) {
      console.log('finished');
    }
  });

I just added a check on the event response.

Hope it helps.

like image 156
soutot Avatar answered Sep 18 '25 08:09

soutot