Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Animated, Complete Event

Tags:

react-native

What's the best practice to trigger an event when Animated.spring finishes?

Animated.spring(this.state.pan, {
    toValue: 0
}).start()

I've searched quite a bit and haven't found a single way to do it. I could use addListener to check if the animation has reached it's end value or a timeout, but they both feel like ugly fixes to what should be super simple.

Does anyone know?

like image 699
August Bjornberg Avatar asked Jun 27 '16 11:06

August Bjornberg


People also ask

How do you check if animation is finished React Native?

The updated answer should be: . start(({finished}) => { if (finished) { console. log('animation ended!) } })

How do I get Animated value React Native?

To get the current value of Animated. Value with React Native, we call addListener on the animated value object. to call spinValue. addListener with a callback to get the current value of the animated value from the value property.

How do you loop animation in React Native?

In order to create looping animations in react native, we should use Animated. loop() method. Wrap your Animations within Animated. loop() and the animations will act as a loop.


2 Answers

Half an hour of googling later and i found this, can't believe they didn't document this better.

https://github.com/facebook/react-native/issues/3212

.start(callback)
like image 54
August Bjornberg Avatar answered Oct 13 '22 13:10

August Bjornberg


This will get fired at the start of the animation

.start(console.log("Start Animation")

Using an arrow function or function, done will get called at the END of the animation

.start(() => {console.log("Animation DONE")})

And finaly this is what it looks like in the context to of a function.

_play(){
  Animated.timing(this.state.progress, {
     toValue: 1,
     duration: 5000,
     easing: Easing.linear,
   }).start(() => {console.log("Animation DONE")});
}

Hope that helps!

like image 30
Sabba Keynejad Avatar answered Oct 13 '22 14:10

Sabba Keynejad