0

I have a UIPageViewController named OnboardingViewController with three viewcontrollers in there. I also have a pageControl which has a tap gesture set on it. My Pageviewcontroller's transitionStyle is scroll and I do not want to change it to pageCurl. I have turned off carousel behavior of the setup by below code where pages is the array of viewcontrollers.

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 
        guard let currentIndex = pages.firstIndex(of: viewController) else { return nil }
        if currentIndex == 0 {
            return nil             
        } else { 
            return pages[currentIndex - 1] 
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let currentIndex = pages.firstIndex(of: viewController) else { return nil }
        if currentIndex < pages.count - 1 {   
            return pages[currentIndex + 1]  
        } else {
            pageControl.currentPage = pages.count - 1
            return nil             
        }
    }

But After that though I am able to stop the circular rotation of all screens I am seeing some bounce when I try to swipe left on my first viewcontroller and right on my last viewcontroller. I tried to prevent it by below way:

for view in self.view.subviews {
    if let scrollView = view as? UIScrollView {
          scrollView.delegate = self
          break
        }
     }

extension OnboardingViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if pageControl.currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width {
                scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if pageControl.currentPage == pages.count - 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 pageControl.currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width {
                scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        } else if pageControl.currentPage == pages.count - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width {
                scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
        }

    }
}

But it is not working and when I am tapping the last dot on pageControl it's not scrolling to the last page. What can I do here?

Istiak Morsalin
  • 10,621
  • 9
  • 33
  • 65
  • If you will only have **three** view controllers, you might want to put them in a normal scroll view and disable bouncing. Otherwise, take a look here: https://stackoverflow.com/questions/21798218/disable-uipageviewcontroller-bounce – DonMag Oct 24 '22 at 19:41

0 Answers0