[英]How do I detect the users touch on an element in react native?
I understand in react native we have: - OnPress (detects a press and release) - OnPressIn (detects the user going from not touching, to touching the button) -OnPressOut (detects the user releasing the button, or moving their finger elsewhere) 我知道在React Native中我们有:-OnPress(检测到按下并释放)-OnPressIn(检测到用户从不触摸到触摸按钮)-OnPressOut(检测到用户释放按钮或将手指移到其他位置)
But I want to detect when the user is already touching the screen elsewhere, and then drags their finger over the button. 但是我想检测用户何时已经在其他地方触摸屏幕,然后将手指拖到按钮上。
You can use the PanResponder
API to have fine-grained control over touches and gestures. 您可以使用
PanResponder
API对触摸和手势进行细粒度的控制。 Create PanHandlers
and attach them to the View
you want to detect touches on. 创建
PanHandlers
并将其附加到要检测触摸的View
。
For your use case of detecting drag, you can use the onPanResponderMove
对于检测拖动的用例,可以使用
onPanResponderMove
onPanResponderMove: (evt, gestureState) => {
// The most recent move distance is gestureState.move{X,Y}
// The accumulated gesture distance since becoming responder is
// gestureState.d{x,y}
}
Use dx
and dy
to get the distance dragged on X and Y axes. 使用
dx
和dy
获取在X和Y轴上拖动的距离。
Here's the documentation on PanResponder
这是
PanResponder
上的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.