首页 > 解决方案 > 如何在本机反应中记录每次更新的动画值?

问题描述

我使用了 Animated Scrollview 并且正在使用我设置为 state 的in道具中的y值来操作其他属性。我也设置了. 但是,我想在状态更新时检查 y 值。我该怎么做呢?如果有多种方法可以做到这一点,很高兴知道,因为我对此很陌生。我确实检查了 addListener 方法,但未能实现它,因为它只记录了初始值 0。我还检查了该方法,但是当设置为 true时它不起作用。contentOffsetonScrolluseNativeDriver to true_getValueuseNativeDriver

您还可以共享资源或代码片段的链接,以供我参考。

如果我错过了问题中的任何细节,请告诉我。谢谢你。

我提到了这个例子:https ://stackoverflow.com/a/41934928/10841628

我的尝试:

state={ y:new Animated.Value(0) }
componentDidMount(){
    this.state.y.addListener(({value}) => this._value = value)
}
componentDidUpdate(){
    console.log(this.state.y._value)
}

和滚动视图:

<Animated.ScrollView 
   scrollEventThrottle={16}
   showsVerticalScrollIndicator={false}
   onScroll={Animated.event(
      [
          {
              nativeEvent: {
                  contentOffset: { y }
              }
          }
      ],
      { useNativeDriver: true }
   )}
/>

标签: javascriptreact-nativereact-animated

解决方案


推荐阅读