UIScrollView disable vertical bounce only at bottom

后端 未结 7 624
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 04:26

I\'ve been searching for a way to disable vertical bounce for a UIScrollView but only at bottom. I still need to have the bounce at the top.

Couldn\'t find anything.

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 04:59

    I have been looking a solution to disable bottom bounce only for a scrollview with wider contentSize width than device since I am using paging. None of those work for me but I found a way to do it. I thought I should share this since I wasted two days just to do this. I am using Swift 4 but this applies to objective c if you know how to convert this which should be easy if you have experience with both.

    This works in a scrollview with device screen frame as frame.

    1. Add variable to be aware for scrollview's current page.
      var currentPage : Int!

    2. Set initial value
      currentPage = 0

    3. Update currentPage after scrolling
      public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { let width = scrollView.frame.size.width let page = (scrollView.contentOffset.x + (0.5 * width)) / width currentPage = Int(page) }

    4. Filter bottom scrolling and update contentOffset
      public func scrollViewDidScroll(_ scrollView: UIScrollView) { if CGFloat(currentPage) * screenWidth < scrollView.contentOffset.x || CGFloat(currentPage) * screenWidth < scrollView.contentOffset.y || (((CGFloat(currentPage) * screenWidth - scrollView.contentOffset.x) >= 0) && scrollView.contentOffset.y > 0){ scrollView.contentOffset.y = 0 } }

    CGFloat(currentPage) * screenWidth < scrollView.contentOffset.x || CGFloat(currentPage) * screenWidth < scrollView.contentOffset.y will filter bottom left and right scrolling

    (((CGFloat(currentPage) * screenWidth - scrollView.contentOffset.x) >= 0) && scrollView.contentOffset.y > 0) will filter bottom mid scrolling

    scrollView.contentOffset.y = 0 will stop it from scrolling

提交回复
热议问题