我正在使用 bool 值来确定 View 的背景颜色
const selectColor = isSelected ? "#8bc34a" : "#e9e9e9";
...
<TouchableOpacity onPress={toggleSelection}>
<View style={{ backgroundColor: selectColor }}>
</View>
</TouchableOpacity>
我希望使用 Animated API 的淡入动画可以改变这个颜色开关。
主要问题是,我的 inputRange 是 bool 值。
感谢您的宝贵时间。
这是你想要的吗?
您可以为 style
对象的 opacity
属性设置动画。您有一个背景颜色为 #e9e9e9
的主 View
和一个背景颜色为 #8bc34a
的嵌套 Animated.View
> 但是一开始opacity是0,toggle时opacity变成1,上面Gif的代码是:
class TestScreen extends Component {
constructor(props) {
super(props);
this.opacity = new Animated.Value(0);
this.toggleBackgroundColor = this.toggleBackgroundColor.bind(this);
}
toggleBackgroundColor() {
Animated.timing(this.opacity, {
toValue: this.opacity._value ? 0 : 1,
duration: 1000
}).start();
}
render() {
return (
<View
style={{
flex: 1, justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#8BC34A'
}}
>
<TouchableOpacity
style={{ borderWidth: 1, borderColor: '#4099FF', zIndex: 1 }}
onPress={this.toggleBackgroundColor}
>
<Text style={{ fontSize: 18, color: '#4099FF', margin: 16 }}>
Toggle
</Text>
</TouchableOpacity>
<Animated.View
style={{
position: 'absolute',
left: 0, right: 0, bottom: 0, top: 0,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#E9E9E9',
opacity: this.opacity
}}
/>
</View>
);
}
}
export default TestScreen;