javascript - 如何停止 react native 动画

标签 javascript reactjs animation react-native

我试图在 React Native 中停止动画,但它不起作用。 我尝试用 stopAnimation method 来做到这一点

这是我的代码:

    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();
        });

    }

在另一个组件中,我触发了 wordGuessed 事件,并且控制台日志有效。我做错了什么?

最佳答案

打电话

Animated.timing(
  this.state.posY
).stop();

关于javascript - 如何停止 react native 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44022964/

相关文章:

javascript - 查看默认情况下是否选中复选框

javascript - 如何修复 React 中隐藏和显示 <div> 的错误

javascript - 为什么我无法在 Tracker.Autorun 中访问 React 状态?

c# - 使用 For 循环滑动对象 (C#)

javascript - 如何使 then 函数工作?

javascript - $(modalEl).modal ('show' ) 和 $(modalEl).modal ('hide' ) 不工作

javascript - 获取所有选定的值()'s from a multiselect container per it'自己点击:event?

javascript - React-create-library 导入图片问题

javascript - 动画化div的居中

javascript - JS : Flying random objects (images)