如何移除 iOS 中的 UIPageViewController 弹跳效果 for Swift 3/4

How to remove UIPageViewController's Bounce effect in iOS for Swift 3/4

这是我的问题的截图:

我已经按照下面的代码完成了。

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<CGPoint>) {
    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)
    }
}

首先在你的 UIPageViewController 中找到 scrollview 并添加 UIScrollViewDelegate

for view in self.pageViewController!.view.subviews {
        if let subView = view as? UIScrollView {
            subView.delegate = self
            subView.isScrollEnabled = true
            subView.bouncesZoom = false

        }
}

(在您的 ViewController class 中扩展 UIScrollViewDelegate)

对于方法 "scrollViewDidScroll" 和 "scrollViewWillEndDragging" 上的 UIScrollViewDelegate,您可以添加如下内容

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
        scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
    }else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
        scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
    }
}

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