react-native - 离开应用程序时 React Native 停止动画
问题描述
我有一个Animated.timing
并且我想在用户导航到另一个应用程序时停止它,这可能吗?
解决方案
你可以AppState
这样使用:
import React, {Component} from "react"
import {AppState, View} from "react-native"
class YourClass extends Component {
state = {
appState: AppState.currentState
}
componentDidMount() {
//add a listener here
AppState.addEventListener("change", this._handleAppStateChange);
}
componentWillUnmount() {
// remember add this line
AppState.removeEventListener("change", this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === "active"){
console.log("App has come to the foreground!")
}else{
//here you can call stop animation function
}
this.setState({appState: nextAppState});
}
render() {
return (
<Text>Current state is: {this.state.appState}</Text>
);
}
}
推荐阅读
- css - 背景图像不重复动态添加的内容
- javascript - 无法将来自套接字 io 的数据作为道具传递给反应组件
- apache-spark - 有条件地获取上一行的值
- javascript - 为什么我不能在angularjs中以两种方式绑定指令组件具有相同的名称?
- python - 如何使用 multiprocessing.pool 传递一个熊猫系列、一个列表和两个常量字符串作为多处理的参数?
- woocommerce - 如果产品在 woocommerce 中有标签,请添加另一个电子邮件收件人
- google-sheets - 谷歌表格使用 arrayFormula 和间接通过地址读取单元格值
- javascript - 将json响应转换为键和值数组javascript
- python - 无法让 PyInstaller 查看已安装软件包的模块
- javascript - 在数值数组的 FilterExpression 中使用 IN 运算符