Reputation: 4448
Here is the screenshot of my issue:
Upvotes: 3
Views: 2237
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
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