Created
October 25, 2016 19:58
-
-
Save Daemon-Devarshi/55f9e72f94a8e13ec2a8c88a0101f928 to your computer and use it in GitHub Desktop.
Plug and play class to implement pagination controller with sample usage
This file contains hidden or 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
// | |
// OnBoardingViewController.swift | |
// | |
// Created by Devarshi Kulshreshtha on 10/26/16. | |
// Copyright © 2016 Devarshi. All rights reserved. | |
// | |
import UIKit | |
class OnBoardingViewController: PaginationViewBaseController { | |
var welcomeViewController: WelcomeViewController! | |
var languageViewController: LanguageViewController! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func configurePageViewController() { | |
let storyboard = UIStoryboard(name: "Main", bundle: nil) | |
pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) | |
pageViewController.dataSource = self | |
welcomeViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController | |
languageViewController = storyboard.instantiateViewController(withIdentifier: "LanguageViewController") as! LanguageViewController | |
paginationContentControllers = [languageViewController, welcomeViewController] | |
super.configurePageViewController() | |
} | |
} |
This file contains hidden or 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
// | |
// PaginationViewBaseController.swift | |
// | |
// Created by Devarshi Kulshreshtha on 10/26/16. | |
// Copyright © 2016 Devarshi. All rights reserved. | |
// | |
import UIKit | |
//This class should be used as base class for PaginationControllers to avoid repetitive code | |
class PaginationViewBaseController: UIViewController { | |
//MARK:- Declarations of vars | |
var pageViewController: UIPageViewController! | |
var paginationContentControllers: [UIViewController]! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
configurePageControl() | |
configurePageViewController() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
//MARK:- Private methods | |
extension PaginationViewBaseController { | |
func configurePageViewController() { | |
pageViewController.setViewControllers([paginationContentControllers[0]], direction: .forward, animated: false, completion: nil) | |
pageViewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height) | |
addChildViewController(pageViewController) | |
view.addSubview(pageViewController.view) | |
pageViewController.didMove(toParentViewController: self) | |
} | |
//TODO: below code and its invocation shall be moved to AppDelegate | |
fileprivate func configurePageControl() { | |
let pageControl = UIPageControl.appearance() | |
pageControl.pageIndicatorTintColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha:1.0) | |
pageControl.currentPageIndicatorTintColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha:1.0) | |
pageControl.backgroundColor = UIColor.white | |
} | |
} | |
//MARK:- UIPageViewControllerDataSource implementation | |
extension PaginationViewBaseController: UIPageViewControllerDataSource { | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
if let index = paginationContentControllers.index(of: viewController){ | |
if index == 0 { | |
return nil | |
} | |
return paginationContentControllers[index - 1] | |
} | |
return nil | |
} | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
if let index = paginationContentControllers.index(of: viewController){ | |
if index == paginationContentControllers.count - 1 { | |
return nil | |
} | |
return paginationContentControllers[index + 1] | |
} | |
return nil | |
} | |
func presentationCount(for pageViewController: UIPageViewController) -> Int { | |
return paginationContentControllers.count | |
} | |
func presentationIndex(for pageViewController: UIPageViewController) -> Int { | |
if let pageContentControllers = pageViewController.viewControllers { | |
let currentPageController = pageContentControllers[0] | |
if let presentationIndex = paginationContentControllers.index(of: currentPageController) { | |
return presentationIndex | |
} | |
return 0 | |
} | |
return 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment