美文网首页react-native
react native 计算组件的绝对位置

react native 计算组件的绝对位置

作者: 冰天无幻 | 来源:发表于2021-04-12 11:18 被阅读0次

第一种的获取组件的绝对位置

import { View,  UIManager,  findNodeHandle } from 'react-native';

<View onLayout={({nativeEvent:e})=>this.layout(e)}> 

layout=(e)=>{

      console.warn(e.layout.y+' 获取绝对位置')  ;

      var UIManager = require('UIManager');

      console.warn(e.target);

      UIManager.measure(e.target, (x, y, width, height, left, top) => {

     console.warn('x:'+x)

     console.warn('y:'+y)

     console.warn('width:'+width)

     console.warn('height:'+height)

     console.warn('left:'+left)

     console.warn('top:'+top)

})

}

第一种缺陷是,如果是组件需要把onLayout 的值给带出来给

https://www.jianshu.com/p/9dba439df799

第二种方法

<View ref = "ref1"/>

layout(){

this.ref1.measure((frameX, frameY, frameWidth, frameHeight, pageX, pageY) => {

//pageX,pageY就是绝对位置

});

}

第二种没试过大家可以试试

https://blog.csdn.net/liubenq/article/details/78499585

相关文章

网友评论

    本文标题:react native 计算组件的绝对位置

    本文链接:https://www.haomeiwen.com/subject/huotlltx.html