Реагировать на собственную анимацию остановки при выходе из приложения - PullRequest
0 голосов
/ 24 декабря 2018

У меня есть Animated.timing, и я хочу остановить его, когда пользователь перейдет к другому приложению, возможно ли это?

1 Ответ

0 голосов
/ 24 декабря 2018

вы можете использовать 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>
        );
     }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...