React Native 获取我的触摸事件的坐标 [英] React native get the coordinates of my touch event

查看:27
本文介绍了React Native 获取我的触摸事件的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得我的触摸事件的坐标.因此,我点击显示屏内的任意位置,就可以检索发生位置的 X、Y 位置.

How would I get the coordinates of my touch event. So I tap anywhere within the display and I can retrieve the X, Y positioning of where it happened.

推荐答案

您需要使用 TouchableOpacity 或其他 Touchable 组件之一包装您的视图对象.使用 TouchableOpacity,您拥有传递新闻事件的 onPress 道具.从此新闻事件中,您可以访问新闻的 x,y 坐标.

You will need to wrap your view object with TouchableOpacity or one of the other Touchable components. With TouchableOpacity you have the onPress prop that is passed a press event. From this press event you have access to the x,y coordinates of the press.

伪代码如下:

render() {
 ....

<TouchableOpacity onPress={(evt) => this.handlePress(evt) } 
 .... > 
 <View></View>
</TouchableOpacity>
}

handlePress(evt){
  console.log(`x coord = ${evt.nativeEvent.locationX}`);
}

这篇关于React Native 获取我的触摸事件的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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