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?
The updated answer should be: . start(({finished}) => { if (finished) { console. log('animation ended!) } })
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.
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.
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)
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With