在 React Native (Expo) 中获取触摸坐标
Getting coordinates of a touch in React Native (Expo)
我需要在 React Native 中获取 click/touch 的(相对)坐标。
我试过使用 RN <View onPress={(e: GestureResponderEvent) => ...}>
在 Web 上,我进一步使用 e.nativeEvent
,它属于 PointerEvent
类型,我使用 layerX, layerY
,它给出了我需要的确切数字。
不幸的是,iOS 本机事件具有名为 locationX
和 locationY
的类似字段。
我必须使用这个丑陋的代码来获取坐标
interface IEvent {
layerX?: number
layerY?: number
locationX?: number
locationY?: number
}
const { layerX, layerY, locationX, locationY } =
e.nativeEvent as unknown as IEvent
const x = layerX ?? locationX
const y = layerY ?? locationY
有没有更好的跨平台方式呢?也许我需要使用另一个控件或另一个事件?
以防万一有人需要它。
我将活动更改为 onPressIn(e)
。 e.nativeEvent
事件在 Web 和 iOS.
中始终使用 locationX
和 locationY
我需要在 React Native 中获取 click/touch 的(相对)坐标。
我试过使用 RN <View onPress={(e: GestureResponderEvent) => ...}>
在 Web 上,我进一步使用 e.nativeEvent
,它属于 PointerEvent
类型,我使用 layerX, layerY
,它给出了我需要的确切数字。
不幸的是,iOS 本机事件具有名为 locationX
和 locationY
的类似字段。
我必须使用这个丑陋的代码来获取坐标
interface IEvent {
layerX?: number
layerY?: number
locationX?: number
locationY?: number
}
const { layerX, layerY, locationX, locationY } =
e.nativeEvent as unknown as IEvent
const x = layerX ?? locationX
const y = layerY ?? locationY
有没有更好的跨平台方式呢?也许我需要使用另一个控件或另一个事件?
以防万一有人需要它。
我将活动更改为 onPressIn(e)
。 e.nativeEvent
事件在 Web 和 iOS.
locationX
和 locationY