Disable UIPageViewController bounce

前端 未结 13 2393
离开以前
离开以前 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:36

    Disable UIPageViewController's bounce

    Swift 2.2

    Addition to answers

    1) Add UIScrollViewDelegate to UIPageViewController

    extension PageViewController: UIScrollViewDelegate
    

    2) Add to viewDidLoad

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

    3) Add UIScrollViewDelegate methods

    func scrollViewDidScroll(scrollView: UIScrollView) {
        if currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if currentIndex == totalViewControllers - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }
    }
    
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
        if currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if currentIndex == totalViewControllers - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }
    }
    

提交回复
热议问题