Jamil Hasnine Tamim
Jamil Hasnine Tamim

Reputation: 4448

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

Here is the screenshot of my issue:

enter image description here

Upvotes: 3

Views: 2237

Answers (2)

Rafiqul Hasan
Rafiqul Hasan

Reputation: 3632

First find scrollview inside your UIPageViewController and add UIScrollViewDelegate

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

        }
}

(Extends UIScrollViewDelegate in your ViewController class)

For UIScrollViewDelegate on method "scrollViewDidScroll" and "scrollViewWillEndDragging" you can add something like this

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

Upvotes: 4

Aleem
Aleem

Reputation: 3291

I've done this like following code.

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

Upvotes: 1

Related Questions