Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop react-native animation

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?

like image 624
Андрей Русев Avatar asked May 17 '17 11:05

Андрей Русев


People also ask

What is React Native animation?

Animations allow you to convey physically believable motion in your interface. React Native provides two complementary animation systems: Animated for granular and interactive control of specific values, and LayoutAnimation for animated global layout transactions.

How do you handle animation in RN?

Working with animations​Animations are started by calling start() on your animation. start() takes a completion callback that will be called when the animation is done.

How do you smooth animation in React Native?

Enabling usage of native driver is the easiest way of improving your animations performance quickly. However, the subset of style props that can be used together with native driver is limited. You can use it with non-layout properties like transforms and opacity. It will not work with colors, height, and others.


1 Answers

Just call

Animated.timing(
  this.state.posY
).stop();
like image 135
Juwan Wheatley Avatar answered Oct 23 '22 18:10

Juwan Wheatley