Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. //
  2. // PageViewController.swift
  3. // UIPageViewController
  4. //
  5. // Created by Apple on 2019/4/14.
  6. // Copyright © 2019 Apple. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Foundation
  11.  
  12. class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  13.  
  14. lazy var VCArr: [UIViewController] = {
  15. return [self.VCInstance(name: "FirstVC"),
  16. self.VCInstance(name: "SecondVC"),
  17. self.VCInstance(name: "ThirdVC")]
  18. }()
  19.  
  20.  
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. self.dataSource = self
  24. self.delegate = self
  25. if let firstVC = VCArr.first{
  26. setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
  27. }
  28. // Do any additional setup after loading the view.
  29. }
  30.  
  31. private func VCInstance(name:String) -> UIViewController{
  32. return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
  33. }
  34.  
  35.  
  36. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  37. guard let viewControllerIndex = VCArr.firstIndex(of: viewController) else{
  38. return nil
  39. }
  40.  
  41. let previousIndex = viewControllerIndex - 1
  42.  
  43. guard previousIndex >= 0 else{
  44. return VCArr.last
  45. }
  46. // 不知道什麼用途
  47. // guard VCArr.count > previousIndex else{
  48. // return nil
  49. // }
  50.  
  51. return VCArr[previousIndex]
  52. }
  53.  
  54. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  55. guard let viewControllerIndex = VCArr.firstIndex(of: viewController) else{
  56. return nil
  57. }
  58.  
  59. let nextIndex = viewControllerIndex + 1
  60.  
  61. guard nextIndex < VCArr.count else{
  62. return VCArr.first
  63. }
  64. // 不知道什麼用途
  65. // guard VCArr.count > nextIndex else{
  66. // return nil
  67. // }
  68.  
  69. return VCArr[nextIndex]
  70. }
  71.  
  72. func presentationCount(for pageViewController: UIPageViewController) -> Int {
  73. return VCArr.count
  74. }
  75.  
  76. func presentationIndex(for pageViewController: UIPageViewController) -> Int {
  77. guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArr.firstIndex(of: firstViewController) else{
  78. return 0
  79. }
  80.  
  81. return firstViewControllerIndex
  82. }
  83.  
  84.  
  85.  
  86. /*
  87. // MARK: - Navigation
  88.  
  89. // In a storyboard-based application, you will often want to do a little preparation before navigation
  90. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  91. // Get the new view controller using segue.destination.
  92. // Pass the selected object to the new view controller.
  93. }
  94. */
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement