ReactNative get the absolute position of a view relative to the screen

后端 未结 2 1453
太阳男子
太阳男子 2021-02-20 08:46

with react native.

I can get the position and size with onLayout, but I want to get the absolute position of a view relative to the screen

please! t

相关标签:
2条回答
  • 2021-02-20 09:18

    You might want to use the "onTouchStart". it Determines the location on screen from pageX, pageY of the given view and returns the values.

    <View
      onTouchStart={(e) => {console.log('touchMove', e.nativeEvent.pageY)}}
     >
    

    a better example can be found here: https://facebook.github.io/react-native/docs/panresponder.html

    0 讨论(0)
  • 2021-02-20 09:22

    you can use measure(callback) it Determines the location on screen, width, and height of the given view and returns the values via an async callback.

    a better example can be found here React Native: Getting the position of an element

    <View
      ref={(ref) => { this.marker = ref }}
      onLayout={({nativeEvent}) => {
        if (this.marker) {
          this.marker.measure((x, y, width, height, pageX, pageY) => {
                    console.log(x, y, width, height, pageX, pageY);
           })
        }
      }}
     >
    
    0 讨论(0)
提交回复
热议问题