Created
August 10, 2017 11:06
-
-
Save omgbbqhaxx/0284e4fcefd60f17d290443bf45c29db to your computer and use it in GitHub Desktop.
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
| // | |
| // PageVC.swift | |
| // PageViewControllerTutorial | |
| // | |
| // Created by Volodymyr Romanov on 10/10/16. | |
| // Copyright © 2016 Vladimir Romanov. All rights reserved. | |
| // | |
| import UIKit | |
| class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { | |
| lazy var VCArr: [UIViewController] = { | |
| return [self.VCInstance(name: "FirstVC"), | |
| self.VCInstance(name: "SecondVC"), | |
| self.VCInstance(name: "ThirdVC")] | |
| }() | |
| private func VCInstance(name: String) -> UIViewController { | |
| return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name) | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.dataSource = self | |
| self.delegate = self | |
| if let firstVC = VCArr.first { | |
| setViewControllers([firstVC], direction: .forward, animated: true, completion: nil) | |
| } | |
| } | |
| override func viewDidLayoutSubviews() { | |
| super.viewDidLayoutSubviews() | |
| for view in self.view.subviews { | |
| if view is UIScrollView { | |
| view.frame = UIScreen.main.bounds | |
| } else if view is UIPageControl { | |
| view.backgroundColor = UIColor.clear | |
| } | |
| } | |
| } | |
| public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
| guard let viewControllerIndex = VCArr.index(of: viewController) else { | |
| return nil | |
| } | |
| let previousIndex = viewControllerIndex - 1 | |
| guard previousIndex >= 0 else { | |
| return VCArr.last | |
| } | |
| guard VCArr.count > previousIndex else { | |
| return nil | |
| } | |
| return VCArr[previousIndex] | |
| } | |
| public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
| guard let viewControllerIndex = VCArr.index(of: viewController) else { | |
| return nil | |
| } | |
| let nextIndex = viewControllerIndex + 1 | |
| guard nextIndex < VCArr.count else { | |
| return VCArr.first | |
| } | |
| guard VCArr.count > nextIndex else { | |
| return nil | |
| } | |
| return VCArr[nextIndex] | |
| } | |
| public func presentationCount(for pageViewController: UIPageViewController) -> Int { | |
| return VCArr.count | |
| } | |
| public func presentationIndex(for pageViewController: UIPageViewController) -> Int { | |
| guard let firstViewController = viewControllers?.first, | |
| let firstViewControllerIndex = VCArr.index(of: firstViewController) else { | |
| return 0 | |
| } | |
| return firstViewControllerIndex | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment