用react写一段动画循环播放的代码
时间: 2023-06-05 08:07:16 浏览: 195
Sure, here is an example of how to create a looped animation in React:
```
import React, { Component } from 'react';
import { Animated, View } from 'react-native';
class Animated extends Component {
state = {
fadeAnim: new Animated.Value(0),
};
componentDidMount() {
Animated.loop(
Animated.sequence([
Animated.timing(this.state.fadeAnim, {
toValue: 1,
duration: 1000,
}),
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: 1000,
}),
]),
).start();
}
render() {
const { fadeAnim } = this.state;
return (
<View style={{ flex: 1 }}>
<Animated.View
style={{
opacity: fadeAnim,
height: 100,
width: 100,
backgroundColor: 'red',
}}
/>
</View>
);
}
}
export default AnimatedLoop;
```
This code will create a simple animation that fades in and out repeatedly. The `componentDidMount()` lifecycle method is used to start the animation sequence, which is looped using the `Animated.loop()` method. The `Animated.timing()` method is used to animate the `fadeAnim` value. The `opacity` of the animated `View` is set to the `fadeAnim` value, causing it to fade in and out.