如何获取AnimatedInterpolation的值

art*_*pse 11 javascript typescript react-native

我的问题是如何在react-native中检索AnimatedInterpolation的值,而不调用私有代码。

我创建动画值并将其包装在插值中,如下所示:

  animated = new Animated.Value();
  interpolated = this.animated.interpolate({
    inputRange:[0, 1000],
    outputRange:[0, 500]
  })
Run Code Online (Sandbox Code Playgroud)

我这样渲染动画:

  <Animated.View style={[{width: this.interpolated}]}/>
Run Code Online (Sandbox Code Playgroud)

我像这样检索动画值:

  this.animated.stopAnimation(v => {
    console.log(v, " ", this.interpolated.__getValue());
  })
Run Code Online (Sandbox Code Playgroud)

可运行的例子在这里

我的问题是,它__getValue()是 AnimatedInterpolation 的私有成员,在使用打字稿时会出错。我正在寻找一种合理的方法来检索该值,类似于如何this.animated.stopAnimation比 更合理this.animated.__getValue()

小智 0

您可以添加一个侦听器interpolated来获取值 -

    interpolated.addListener(({value}) => {
      console.log('interpolated', value)
    });
Run Code Online (Sandbox Code Playgroud)

然后你可以将它与变量或任何东西一起使用。