I have an animation consisting of a sequence of images: image01.png
, image02.png
, image03.png
, etc. How do I get these to animate continuously on 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.
Interpolation is a way of estimating a function at intermediate points, learning from the ranges you provide. In React Native, you can interpolate animated values using . interpolate which takes an inputRange, that interpolates and maps the values to an outputRange.
You can try with libraries:
First is more more efficient, second is in pure javascript. Another way is to implement it on your own way, like here: https://github.com/facebook/react-native/issues/9280
It should looks like this
export default class Animation extends Component {
constructor(props) {
super(props);
this.images = [
require('./img_01.png'),
require('./img_02.png'),
require('./img_03.png'),
];
this.next = this.next.bind(this);
this.state = {index: 0};
}
componentDidMount() {
this.next();
}
next() {
setTimeout(() => {
this.setState({index: (this.state.index+1)%3});
this.next();
}, 300);
}
render() {
return (
<Image
source={this.images[this.state.index]}
style={styles.image}
/>
)
}
}
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