相关疑难解决方法(0)

如何在屏幕上获取组件的位置以反应原生?

我正在开发一个本机应用程序,我想在屏幕上处理触摸.

一个用例是当用户在屏幕上"按下"时,我希望能够获得屏幕上特定组件的位置(x,y)以知道它是否与触摸的(x,y)匹配.

我已经搜索了堆栈溢出,但没有一个给定的解决方案有效...

在我的根组件中:

_onPress = () => {
    // How can I get the position of my component ?
    this._myComponent.xxx();
};

render() {
    return (
        <View><MyComponent ref={(r) => this._myComponent = r;} /></View>
    );
}
Run Code Online (Sandbox Code Playgroud)

编辑: 尝试此解决方案后(React Native:获取元素的位置)我使其工作如下:

在MyComponent.js中:

getPosition () => {
    this._ref._component.measure((width, height, px, py, fx, fy) => {
        const location = {
            fx: fx,
            fy: fy,
            px: px,
            py: py,
            width: width,
            height: height
        }
        console.log(location)
    });
};

render() {
    return (
        <View ref={(r) => …
Run Code Online (Sandbox Code Playgroud)

javascript react-native

5
推荐指数
2
解决办法
5008
查看次数

标签 统计

javascript ×1

react-native ×1