react-native - 5 秒后褪色 React Native 元素
问题描述
我的 React Native 屏幕中有一个View
我想显示的,然后在 5 秒后淡出。这是方法内部的视图render()
:
{this.state.showView (
<Animated.View style={{ opacity: this.state.fadeAnim }}>
</Animated.View>
)}
屏幕上有一个按钮,当我点击按钮时,我想在显示和隐藏视图之间切换。这里没有任何动画,但是在视图显示 5 秒后,我想将其淡出。这是我在onPress()
按钮事件中所拥有的。
onPress = () => {
if (this.state.showView) {
this.setState({
...this.state,
fadeAnim: new Animated.Value(0),
showView: false,
});
} else {
this.setState(
{
...this.state,
fadeAnim: new Animated.Value(1),
showView: true,
},
() => {
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: 2000,
delay: 5000,
}).start(() => {
this.setState({
...this.state,
fadeAnim: new Animated.Value(0),
showView: false,
});
});
},
);
}
};
这行不通。视图按预期显示和消失,但不会淡出;它只是在没有动画的情况下消失。我究竟做错了什么?
解决方案
推荐阅读
- javascript - 如何使用nodejs在pdf中添加水印?
- json - Python JSON Parsing'列表索引必须是整数'
- html - 如何将 HTML 行保持在 80 个字符以下?
- html - 如何使 Bootstrap “table-bordered” 固定标题和滚动体?
- java - 如何在 Java 类中使用 @Bean 配置数据源
- unix - 使用 ssh 检查远程 scp 服务器上的文件的 shell 脚本
- angular - Angular:ngrx subscribe 不会更新视图
- python-3.5 - 使自定义 odoo 9 模块中的开发功能在 odoo 11 中工作
- javascript - WEB MIDI + CSS 变量
- hibernate - org.hibernate.MappingException:在实体 [ab.cdef.ghijklm.db.entity.RefOnlineShop] 上找不到属性 [xxx]