Animate.spring 完成后调用函数

IT技术 reactjs react-native react-native-ios
2021-05-26 17:13:47

我正在使用动画以便从右侧弹出一个弹出窗口。我为此使用以下代码 -

  var toValue = 200;

  if(this.state.fileMenu){
    toValue = 0;
  }

  Animated.spring(
    this.state.bounceValue,
    {
      toValue: toValue,
      velocity: 3,
      tension: 2,
      friction: 5,
    }
  ).start();
  //I want to call a function here after the animation is finished.

  this.setState({fileMenu: !this.state.fileMenu});

代码运行完美,看起来很棒,但是,我现在只想在动画完全完成时调用一个函数,并且严格只有一次。只是想知道我怎么能做到这一点?不确定这是否是一个非常简单的问题。

1个回答

start 带一个回调函数,该函数在动画结束后执行一次:

 Animated.spring(
    this.state.bounceValue,
    {
      toValue: toValue,
      velocity: 3,
      tension: 2,
      friction: 5,
    }
  ).start(() => doSmething());