简体   繁体   中英

iOS UIPageControl change behavior when tap left and right

I am using default UIPageControl . UIPageViewController is created programmatically. The dataSource and delegate methods are also configured correctly.

I am trying to implement the same behaviour as in the iOS home page.

Tapping right should take to the next page. Tapping left should take to the previous page.

These behaviours are default. and everything works fine except when tapped right on the last page, it takes to first page. ie infinite loop. same for the first page and left tap, it takes to last page.

I have prevented infinite loop in the viewControllerBefore and viewControllerAfter methods for swipe gesture.

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?

Findings:

  • When tapped on the right, on the last page, calls viewControllerBefore
  • When tapped on the left, on the first page, calls viewControllerAfter

Is there anyway to handle this case? I see the expected behaviour in the iOS home screen.

Just return nil in the above mentioned method when the index of your pages are either 0 or n - 1 . Where n is the total page count.

Example:

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    // Check if index is zero, then return nil   
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        // Check if index is equal to total page count, then return nil   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM