小编pan*_*149的帖子

使用React Native Animated淡入和淡出元素

我是刚接触原生动画功能的新手。我希望使用它来淡入和淡出按钮时的元素。下面的代码可在页面加载时在元素中淡入淡出,但是淡出该按钮的按钮并不会像我期望的那样使其淡出。有人可以解释我在做什么错。谢谢。

class FadeComponent extends Component {

  constructor(props) {
    super(props)
    this.state = {
      fadeAnim: new Animated.Value(0),  // Initial value for opacity: 0
    }
    this.fadeOut = this.fadeOut.bind(this);
  }

  componentDidMount() {
    Animated.timing(          // Animate over time
      this.state.fadeAnim,    // The animated value to drive
      {
        toValue: 1,           // Animate to opacity: 1 (opaque)
        duration: 2000,       // 2000ms
      }
    ).start();                // Starts the animation
  }

  fadeOut(){
    this.setState({fadeAnim: new Animated.Value(1)})
    Animated.timing(          // Animate over time
      this.state.fadeAnim, // The animated value to drive
      {
        toValue: 0,           // …
Run Code Online (Sandbox Code Playgroud)

react-native

4
推荐指数
1
解决办法
5585
查看次数

标签 统计

react-native ×1