javascript - 如何在 React Native 中的两个颜色动画循环之间平滑过渡?
问题描述
假设我在绿色和蓝色之间运行动画循环(沿着插值有更多颜色,但我开始在前三个之间循环):
this.state = {
colorValue: new Animated.Value(0)
};
this.interpolations = {
background: this.state.colorValue.interpolate({
inputRange: [0, 25, 50, 75, 100],
outputRange: ["#64e5a5", "#216a7a", "#64e5a5", "#ffb637", "#ffd338"]
})
};
this.backgroundAnimation = Animated.loop(
Animated.timing(this.state.colorValue, {
toValue: 50,
duration: 10000
})
);
this.backgroundAnimation.start();
请注意,我沿着插值设置了额外的颜色,希望我可以停止动画循环并开始一个新的循环,在 75 和 100 之间循环,以便在两个颜色循环之间平滑过渡。当我修改此代码以获得所需的效果时,动画似乎总是从 0 开始。我尝试将动画值设置为 50,但这并没有解决任何问题。有人可以解释这样做的正确方法吗?
注意:上面的代码不包括实际切换到下一个颜色循环的尝试,因为我真的不知道如何去做。
编辑:我快接近了,但不是很接近,第二个循环不起作用:
this.backgroundAnimation.stop();
this.interpolations.background = this.state.colorValue.interpolate({
inputRange: [0, 100],
outputRange: [JSON.stringify(this.interpolations.background), "#ffd338"]
});
Animated.timing(this.state.colorValue, {
toValue: 100,
duration: 3000
}).start(() => {
console.log("Done");
this.setState({
...this.state,
colorValue : new Animated.Value(0) //just spit-balling, idk
});
this.interpolations.background = this.state.colorValue.interpolate({
inputRange: [0, 50, 100],
outputRange: ["#ffd338", "#fff39b", "#ffd338"]
});
console.log(this.interpolations.background);
this.backgroundAnimation = Animated.loop(
Animated.timing(this.state.colorValue, {
toValue: 100,
duration: 3000
})
);
this.backgroundAnimation.start();
});
解决方案
弄清楚了 :)
this.backgroundAnimation.stop();
this.setState({
...this.state,
interpolations: {
...this.state.interpolations,
background: this.state.colorValue.interpolate({
inputRange: [0, 100],
outputRange: [
JSON.stringify(this.state.interpolations.background),
"#ffd338"
]
})
}
});
Animated.timing(this.state.colorValue, {
toValue: 100,
duration: 2000
}).start(() => {
this.setState({
...this.state,
interpolations: {
...this.state.interpolations,
background: this.state.colorValue.interpolate({
inputRange: [0, 50, 100],
outputRange: ["#ffd338", "#ffffff", "#ffd338"]
})
}
});
this.backgroundAnimation = Animated.loop(
Animated.timing(this.state.colorValue, {
toValue: 100,
duration: 2000
})
);
this.backgroundAnimation.start();
});
});
推荐阅读
- ruby-on-rails - 如何在 ruby 和 ruby on rails 中只允许使用 preg_replace 的数字字符
- python - 计算值在某个值范围内出现的次数
- mysql - mysql -uroot 随机失败/成功
- swift - 无法在本地通知中将录制的剪辑用作声音
- c# - 在 C# 控制台应用程序中同步返回任务的方法
- excel - VBA,从A列到J中清除选定行的内容
- mbed - 如何让我的电脑通过以太网与 mbed NXP LPC 1768 通信
- sql-server - 使用 T/SQL FOR XML 技巧连接时如何对值中的分隔符进行实体编码
- java - 获取 WSDL 文件中提供的操作的输入参数名称 | 肥皂 | 爪哇
- html - 禁用 HTML 中的标记