欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS UIScrollView和控制器返回手势冲突解决方法

程序员文章站 2022-07-06 11:41:07
开发中,有部分ui,会将uiscrollview横向铺在底层,上面放tableview 或一些视图左右滚动切换,底层的scrollview会和nav viewcontroller原有的返回手势冲突解决...

开发中,有部分ui,会将uiscrollview横向铺在底层,上面放tableview 或一些视图左右滚动切换,底层的scrollview会和nav viewcontroller原有的返回手势冲突 

iOS UIScrollView和控制器返回手势冲突解决方法

解决办法,重写uiscrollview 的gesturerecognizershouldbegin,在scrollview滚动到头的时候,屏蔽scrollview的手势

class gesturesconflictscrollview: uiscrollview {
  override func gesturerecognizershouldbegin(_ gesturerecognizer: uigesturerecognizer) -> bool {
    back(by: gesturerecognizer)
  }

  private final func back(by gesturerecognizer: uigesturerecognizer) -> bool {
    
    guard gesturerecognizer == pangesturerecognizer else { return true }
    // point.x < 0 代表左滑即手指从屏幕右向左移动 反之一样
    let point: cgpoint = pangesturerecognizer.translation(in: self)
    let state: uigesturerecognizer.state = gesturerecognizer.state
    let locdistance: cgfloat = uiscreen.main.bounds.size.width
    
    if state == .began || state == .possible {
      let locationpoint = gesturerecognizer.location(in: self)
      if point.x > 0 && locationpoint.x < locdistance && contentoffset.x <= 0 {
        return false
      }
      let pagecount = contentsize.width / uiscreen.main.bounds.size.width
      let criticalpoint = pagecount < 2 ? locdistance : locdistance * (pagecount - 1)
      if point.x < 0 && contentoffset.x == criticalpoint {
        return false
      }
    }
    return true
  }
}

到此这篇关于ios uiscrollview和控制器返回手势冲突解决方法的文章就介绍到这了,更多相关ios uiscrollview和控制器手势冲突内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!