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

3
这是我的问题截图: enter image description here
2个回答

4

首先,在您的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

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

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
我已经按照以下代码完成了这个任务。
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)
    }
}

谢谢你的回答! - Jamil Hasnine Tamim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接