首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅当手指移动超过屏幕的20%时才进行滚动

仅当手指移动超过屏幕的20%时才进行滚动
EN

Stack Overflow用户
提问于 2020-07-13 14:28:35
回答 1查看 139关注 0票数 1

我有一个滚动视图,其中包含子项的列表(Webview-垂直滚动)。当我尝试垂直滚动Webview时,出现偏转或在父级水平方向上的小移动。我试着将消息从子进程传递给父进程,以禁用或启用滚动,这很慢,也不是100%完美。有没有办法通过react-native-scrollview for android中的道具来实现这一点?

谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-13 22:09:49

我通过使用react-native的PanResponder找到了我的问题的答案。

代码语言:javascript
复制
componentWillMount() {
  this._panResponder = PanResponder.create({
    onMoveShouldSetPanResponder: (evt, gestureState) => {
      return Math.abs(gestureState.dx) > minDragWidth;
    },
    onPanResponderMove: (evt, gestureState) => {
      if(!this._swipeDirection) this.checkSwipeDirection(gestureState);
    },
    onPanResponderRelease: (evt, gestureState) => {
      this._swipeDirection = null;
    },
  });
}

因此,当启动触摸事件和继续拖动时,将分别调用这些函数。

通过检查滚动是水平的还是垂直的,我们可以执行相应的操作。

代码语言:javascript
复制
checkSwipeDirection(gestureState) {
  const isHorizontalSwipe = (Math.abs(gestureState.dx) > Math.abs(gestureState.dy * 3)) && 
  (Math.abs(gestureState.vx) > Math.abs(gestureState.vy * 3));

  if(isHorizontalSwipe) {
      this._swipeDirection = "horizontal";
      let index = (gestureState.dx > 0) ? this.state.paginationIndex - 1 : 
this.state.paginationIndex + 1;
      index = (index > 0) ? (index > (this.props.data.length - 1) ? (index - 1) : index) : 0;
      this._scrollToIndex(index, true);
      this.props.doRequiredAction({ index });
  } else {
      this._swipeDirection = "vertical";
  }
}

参考this post。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62870320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档