ReactNative 获取视图相对于屏幕的绝对位置 [英] ReactNative get the absolute position of a view relative to the screen

查看:108
本文介绍了ReactNative 获取视图相对于屏幕的绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与本机反应.

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

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! the absolute position of a view relative to the screen

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

not the absolute position of a view relative to the parent view

我该怎么办?

推荐答案

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

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.

可以在此处找到更好的示例 React Native:获取元素的位置

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);
       })
    }
  }}
 >

这篇关于ReactNative 获取视图相对于屏幕的绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆