How to stop react-native animation
Asked Answered
F

2

14

I am trying to stop animation in react native, but it does not work. I try to do this with stopAnimation method

This is my code:

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

    }

In the other component I am firing wordGuessed event, and the console log works. What I am doing wrong?

Firebrick answered 17/5, 2017 at 11:0 Comment(0)
T
39

Just call

Animated.timing(
  this.state.posY
).stop();
Tiffie answered 17/5, 2017 at 13:12 Comment(1)
Animated.timing expects a second configuration parameterPyonephritis
I
0

From https://www.codedaily.io/courses/React-Native-Animated-for-Beginners/stopAnimation

this._animatedValue = new Animated.Value(0);

Animated.timing(this._animatedValue, {
  toValue: 100,
  duration: 500,
}).start();

setTimeout(
  () =>
    this._animatedValue.stopAnimation(({ value }) =>
      console.log("Final Value: " + value)
    ),
  250
);
Indefinite answered 7/4, 2024 at 1:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.