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

时间:2017-09-14 10:10:58

标签: react-native

with react native。

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

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

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

我该怎么办?

2 个答案:

答案 0 :(得分:14)

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

可在此处找到更好的示例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);
       })
    }
  }}
 >

答案 1 :(得分:-2)

您可能要使用“ onTouchStart”。它根据给定视图的pageX,pageY确定屏幕上的位置并返回值。

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

一个更好的例子可以在这里找到:   https://facebook.github.io/react-native/docs/panresponder.html