Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ProductCategoriesPageViewController.swift
import UIKit
class ProductCategoriesPageViewController: UIPageViewController {
// MARK: Properties
var categoriesViewPageDelegate: ProductCategoriesPageDelegate?
var pages = [UIViewController]()
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: ProductCategoriesViewController setup methods
func setupPages(with viewControllers: [UIViewController?], andShow firstPage: Int?) {
dataSource = self
delegate = self
for viewController in viewControllers {
guard let viewController = viewController else { break }
pages.append(viewController)
}
let firstPage = firstPage ?? 0
setViewControllers([pages[firstPage]], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func showViewController(for firstPage: Int?) {
let firstPage = firstPage ?? 0
setViewControllers([pages[firstPage]], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
}
extension ProductCategoriesPageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = viewController.view.tag
if (index == 0) || (index == NSNotFound) {
return nil
}
index -= 1;
return pages[index]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = viewController.view.tag
if index == NSNotFound {
return nil;
}
index += 1;
if index == pages.count {
return nil;
}
return pages[index]
}
}
extension ProductCategoriesPageViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
guard let index = pageViewController.viewControllers?.last?.view.tag else { return }
categoriesViewPageDelegate?.onPageChanged(index)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment