如何停止反应原生动画

Анд*_*сев 7 javascript animation reactjs react-native

我试图停止反应原生动画,但它不起作用.我试着用stopAnimation方法做到这一点

这是我的代码:

    constructor(props) {
        super(props);
        //...
        this.state = {
            posY: new Animated.Value(0),
            posX: new Animated.Value(0),
            //...
        };
    }

    componentWillMount(){
        //...
        let eventEmitter = getGlobalEventEmitter();

        eventEmitter.emit('initialize', {words : true});
        eventEmitter.addListener('startGame', ()=>{
            this.setState({initialized: true});
            this.updateText();
        });
    }

    updateText(){

        let currentText = [];
        //... set some values to currentText

        this.props.setText(currentText); // store in redux
        this.startText(this.effects[textEffect]['duration']);
    }

    startText(duration) {

        let viewHeight = 530;
        let fallTo = 500;


        Animated.timing(
            this.state.posY,
            {
                toValue: fallTo,
                duration: duration
            }
        ).start();


        let stopAnimation = function(){
            this.state.posY.stopAnimation();
            console.log("ANIMATION SHOULD STOP");
        };
        stopAnimation = stopAnimation.bind(this);

        eventEmitter.addListener('wordGuessed', ()=>{            
            stopAnimation();
        });

    }
Run Code Online (Sandbox Code Playgroud)

在另一个组件中,我正在触发wordGuessed事件,并且控制台日志正常工作.我做错了什么?

小智 24

打电话吧

Animated.timing(
  this.state.posY
).stop();
Run Code Online (Sandbox Code Playgroud)

  • Animated.timing 需要第二个配置参数 (6认同)