Disable UIPageViewController bounce

前端 未结 13 2431
离开以前
离开以前 2020-11-27 04:08

Searched a lot for this one, but couldn\'t find a proper solution yet.

Is it possible to disable the bounce effect of a UIPageViewController and still

13条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 04:44

    @Dong Ma's approach is perfect but it can be a little bit improved and simplified.

    Code to put into viewDidLoad:

    for subview in view.subviews {
        if let scrollView = subview as? UIScrollView {
            scrollView.delegate = self
            break
        }
    }
    

    Implementation for scrollViewDidScroll:

    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width) || (currentPage == totalNumberOfPages - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
          scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }
      }
    

    Implementation for scrollViewWillEndDragging:

    public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
        if (currentPage == 0 && scrollView.contentOffset.x <= scrollView.bounds.size.width) || (currentPage == totalNumberOfPages - 1 && scrollView.contentOffset.x >= scrollView.bounds.size.width) {
          targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }
      }
    

提交回复
热议问题