Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fade Out Text automatically after some time by using react-native Animation

By using following code I am able to fade in some text,

class AnimatedClass extends Component {
   constructor(props) {
      super(props);

      this.state = {fadeIn: new Animated.Value(0),
                    fadeOut: new Animated.Value(1),
                   };
   }

   fadeIn() {
     this.state.fadeIn.setValue(0)                  
     Animated.timing(
       this.state.fadeIn,           
       {
         toValue: 1,                   
         duration: 3000,              
       }
     ).start(() => this.fadeOut());                        
  }

  fadeOut() {
    this.state.fadeOut.setValue(1)
    Animated.timing(                  
       this.state.fadeOut,            
       {
         toValue: 0,                   
         duration: 3000,              
       }
    ).start();                        
  }

  render() {
    return(
       <View style={{flex: 1, backgroundColor: '#efefef'}}>
           <TouchableOpacity 
               onPress={() => this.fadeIn()} 
               style={Styles.submitButtonStyle}
               activeOpacity={0.5}
           >
               <Text style={Styles.submitTextStyle}>Submit</Text>
           </TouchableOpacity>

           <Animated.View                 
              style={{opacity: this.state.fadeIn}}
           >
              <View style={Styles.textContainer}>
                <Text style={{fontSize: 20, textAlign: 'center'}}>Your order has been submitted</Text>
             </View>
           </Animated.View>
       </View>
   );

 }
}

But, Anyway Text is not getting faded out automatically after some time. In fact, I set time after how many seconds Text should be faded out. But it's not working.

Any help is appreciated. Thanks in Advance.

like image 788
Mighty Avatar asked Feb 19 '18 13:02

Mighty


People also ask

What is fade in and fade out in animation?

The Fade In/Fade Out behavior lets you dissolve into and out of any object by ramping the opacity of the object from 0 percent to 100 percent at the start, and then back to 0 percent at the end. You can eliminate the fade-in or fade-out effect by setting the duration of the Fade In Time or Fade Out Time to 0 frames.

How do you use the transition effect in React Native?

The transition animation is one of the easiest ways to easily animate a React Native application. It provides a way to create a seamless change between various states in your application. In this tutorial, we will be exploring the Transition API and a use-case where the transition animation can be employed; Dark mode.


1 Answers

You're basing the opacity on this.state.fadeIn but modifying this.state.fadeOut in your fadeOut().

Try:

fadeOut() {
    Animated.timing(                  
       this.state.fadeIn,            
       {
         toValue: 0,                   
         duration: 3000,              
       }
    ).start();                        
  }

and maybe change the variable name to just fadeValue or something more clear for future you.

like image 56
MattyK14 Avatar answered Sep 23 '22 08:09

MattyK14