使用原生的react。
我可以使用onLayout获得位置和大小,但我希望获得视图相对于屏幕的绝对位置
请!视图相对于屏幕的绝对位置
视图相对于父视图的绝对位置
我该怎么办?
发布于 2017-09-14 22:57:10
您可以使用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);
})
}
}}
>发布于 2018-11-15 18:57:13
您可能想要使用"onTouchStart“。它根据给定视图的pageX和pageY确定屏幕上的位置,并返回值。
<View
onTouchStart={(e) => {console.log('touchMove', e.nativeEvent.pageY)}}
>在这里可以找到一个更好的例子:https://facebook.github.io/react-native/docs/panresponder.html
https://stackoverflow.com/questions/46216325
复制相似问题