如何在iOS中为Swift 3/4删除UIPageViewController的Bounce效果

时间:2018-03-12 06:32:00

标签: ios iphone swift uipageviewcontroller bounce

以下是我的问题的截图:

enter image description here

2 个答案:

答案 0 :(得分:3)

首先在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类中扩展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)
    }
}

答案 1 :(得分:1)

我已按照以下代码完成此操作。

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