I have a View
in my React Native screen that I want to display and then fade out after 5 seconds. Here's the view inside the render()
method:
{this.state.showView (
<Animated.View style={{ opacity: this.state.fadeAnim }}>
</Animated.View>
)}
There's a button on the screen, when I tap on the button, I want to toggle between displaying and hiding the view. Nothing is animated here, but 5 seconds after the view is displayed, I want to fade it out. Here's what I have in the onPress()
event of the button.
onPress = () => {
if (this.state.showView) {
this.setState({
...this.state,
fadeAnim: new Animated.Value(0),
showView: false,
});
} else {
this.setState(
{
...this.state,
fadeAnim: new Animated.Value(1),
showView: true,
},
() => {
Animated.timing(this.state.fadeAnim, {
toValue: 0,
duration: 2000,
delay: 5000,
}).start(() => {
this.setState({
...this.state,
fadeAnim: new Animated.Value(0),
showView: false,
});
});
},
);
}
};
This doesn't work. The view is displayed and disappeared as expected, but it doesn't fade out; it just disappears without animation. What am I doing wrong?