React native 使用动画变换时,无法触发onLayout

React native 使用动画变换时,无法触发onLayout,react-native,React Native,我的代码渲染一个红方块并使用PanResponder移动。释放pan后,它通过动画变换移动。 但是当我拖动正方形时,onLayout()不会触发。 我想随时得到正方形的位置,即使是在动画中。 有什么问题 <View style={styles.container}> <Animated.View style={[ styles.box, { transform: [ { transl

我的代码渲染一个红方块并使用PanResponder移动。释放pan后,它通过动画变换移动。 但是当我拖动正方形时,onLayout()不会触发。 我想随时得到正方形的位置,即使是在动画中。 有什么问题

<View style={styles.container}>
    <Animated.View
      style={[
        styles.box,
        {
          transform: [
            { translateX: this._animatedValue.x },
            { translateY: this._animatedValue.y },
          ],
          backgroundColor: 'red'
        },
      ]}
      {...this._panResponder.panHandlers}
      onLayout={({ nativeEvent }) => { console.log(nativeEvent.layout) }}
    />
  </View>

{console.log(nativeEvent.layout)}
/>

谢谢

如果您随时想要当前位置,并且您正在使用PanResponder移动跟踪用户移动方块时的触摸,我建议您在
Animated.event()
上使用侦听器,我假设您正在传递给您的
onPanResponderMove
处理程序

看起来像

Animated.event([null, {dx: this._animatedValue.x, dy: this._animatedValue.y}], {
  listener: ({nativeEvent}) => {
    //nativeEvent has your x and y position in it
  }
})
下面是上的React Native docs,其中有一个示例显示如何在PanResponder中使用它,尽管它们只在X方向上跟踪