react-native 点击ScrollView时如何获取点击点相对于ScrollView的坐标

Eq4Lj.jpg

如上图,有一个ScrollView,里面有许多组件component(1, 2...),图中的红点是我点击的位置。如何获取点击点的坐标(x1, y1)和(x2, y2)?

我给ScrollView加了panResponder手势,在onPanResponderGrant时,可以拿到event和gestureState两个对象。根据rn的文档描述:

locationX - The X position of the touch, relative to the element
locationY - The Y position of the touch, relative to the element
pageX - The X position of the touch, relative to the root element
pageY - The Y position of the touch, relative to the root element

我尝试了log这些值,发现locationX和locationY是被点击组件的相对坐标。比如当我点中图中component3时,得到的locationX和locationY并不是相对于ScrollView的坐标,而是相对于component3的。。。

有没有什么办法在点击ScrollView时,获取点击点相对于ScrollView的坐标?

阅读 4.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题