With animated transform,onLayout cannot be triggered

ⅰ亾dé卋堺 提交于 2021-02-19 05:41:05

问题


My codes render a red square and moved using PanResponder.After release pan ,it move by animation transform. But onLayout() does not trigger when I drag the square. I want to get the square location anytime even in animating. What is the problem?

<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>

Thanks!


回答1:


If you want the current position at any time, and you're using a PanResponder to move track the user's touch while they move the square, I would suggest using a listener on your Animated.event() that I'm assuming you're passing to your onPanResponderMove handler.

It would look something like

Animated.event([null, {dx: this._animatedValue.x, dy: this._animatedValue.y}], {
  listener: ({nativeEvent}) => {
    //nativeEvent has your x and y position in it
  }
})

Here's the React Native docs on Animated.event(), which have an example showing how to use it in a PanResponder, though they're only tracking on the X direction.



来源:https://stackoverflow.com/questions/51275196/with-animated-transform-onlayout-cannot-be-triggered

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!