我在启动画面中使用了 react-native-linear-gradient
我已经设置了 4 种不同的颜色,并使其成为白色渐变。
const gradient1 = [Colors.secondary1Color, Colors.white];
const gradient2 = [Colors.primary1Color, Colors.white];
const gradient3 = [Colors.secondaryColor, Colors.white];
const gradient4 = [Colors.primaryColor, Colors.white];
我先设置this.state = {changeGradient: true}
然后我有这个 changeGradient 函数,它可以改变 const 渐变颜色曾经有过第二次
changeGradient = async () => {
console.log("changeGradient", await this.state.gradient);
if (this.state.changeGradient) {
setTimeout(async () => {
await this.setState(({ gradient }) => ({ gradient: gradient === gradient1 ? gradient2 : gradient === gradient2 ? gradient3 : gradient === gradient3 ? gradient4 : gradient1 }));
this.changeGradient();
}, 500);
}
};
然后,我在Lineargradient 组件中进行渲染
<LinearGradient colors={this.state.gradient} style={styles.container}>
</LinearGradient >
你可以走了!!!
希望对您有所帮助!