ReactNative获取视图相对于屏幕的绝对位置

wei*_*ang 8 react-native

与原生作出反应.

我可以获得位置和大小onLayout,但我想获得相对于屏幕的视图的绝对位置

请!视图相对于屏幕的绝对位置

不是视图相对于父视图的绝对位置

我该怎么办?

Moh*_*lil 18

你可以使用measure(回调)它确定给定视图的屏幕,宽度和高度的位置,并通过异步回调返回值.

一个更好的例子可以在这里找到React Native:获取元素的位置

<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);
       })
    }
  }}
 >
Run Code Online (Sandbox Code Playgroud)

  • 只给我`undefined`,有和没有setTimeout,作为参数:/ (2认同)