Created
April 11, 2017 19:47
-
-
Save gracietti/8ad9a66aadfe71c70685d7a272a35a80 to your computer and use it in GitHub Desktop.
ProductCategoriesPageViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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