Parent Component:
routes.forEach((data, index) => {
content.push(<Item key={index} offset={688} route={route} />)
})
Item
Component:
scrollAnimate (toValue) {
const { offset } = this.props;
Animated.timing(
this.state.xTranslate,
{
toValue,
duration: 20000,
easing: Easing.linear,
useNativeDriver: true
}
).start((e) => {
if (e.finished) {
const newState = {xTranslate: new Animated.Value(offset)}
this.setState(newState, () => { this.scrollAnimate(toValue) })
}
});
}
I want every Item
Component loop animate separate, but the fact is that all Item
Components animation end and then all Item
Component start the animation together. How can I fix it?
If an animation is in process of being animated and, for any particular reason, you need to stop it, you can call stopAnimation . The stopAnimation call also takes a callback with the value that the animation was stopped on. this. _animatedValue = new Animated.
The updated answer should be: . start(({finished}) => { if (finished) { console. log('animation ended!) } })
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.
In React Native, you can interpolate animated values using . interpolate which takes an inputRange, that interpolates and maps the values to an outputRange. Great thing is that you can interpolate numbers not just to other numbers but also to colours and degrees.
Well it looks like all your animations start at the same time and have the same duration so obviously they will end at the same time.
You can give them different duration or add different delays if you want to prevent them from being synchronized:
Animated.timing(
this.state.xTranslate,
{
toValue,
duration: 20000,
easing: Easing.linear,
useNativeDriver: true,
delay: Math.random() * 1000, // Or pass it as this.props.delay
}
)
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