使用react native PanResponder,当屏幕触摸坐标超出某个值范围时,如何阻止移动?

例如,如何防止用户将组件移动到屏幕上某个y位置以下?

PanResponder使用Gesture Responder System

我正在仔细阅读文档,但找不到答案。

任何帮助是极大的赞赏。

最佳答案

查看您提供的PanResponder文档页面(https://facebook.github.io/react-native/docs/panresponder.html),我认为您可以修改该示例以满足您的需求。

负责响应手势的 Action 的函数是PanResponder的onPanResponderMove属性,在文档的示例代码中如下所示:

_handlePanResponderMove: function(e: Object, gestureState: Object) {
    this._circleStyles.style.left = this._previousLeft + gestureState.dx;
    this._circleStyles.style.top = this._previousTop + gestureState.dy;
    this._updateNativeStyles();
},

手势状态对象如下所示:
stateID - ID of the gestureState- persisted as long as there at least one touch on screen
moveX - the latest screen coordinates of the recently-moved touch
moveY - the latest screen coordinates of the recently-moved touch
x0 - the screen coordinates of the responder grant
y0 - the screen coordinates of the responder grant
dx - accumulated distance of the gesture since the touch started
dy - accumulated distance of the gesture since the touch started
vx - current velocity of the gesture
vy - current velocity of the gesture
numberActiveTouches - Number of touches currently on screen

您可能会在_handlePanResponderMove中添加条件检查,以确定gestureState.y0是否低于某个阈值,并且只有在此情况下才应用更改

关于react-native - 使用PanResponder在React Native中锁定运动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43564565/

10-10 03:05