Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PageViewController.swift
- // UIPageViewController
- //
- // Created by Apple on 2019/4/14.
- // Copyright © 2019 Apple. All rights reserved.
- //
- import UIKit
- import Foundation
- class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
- lazy var VCArr: [UIViewController] = {
- return [self.VCInstance(name: "FirstVC"),
- self.VCInstance(name: "SecondVC"),
- self.VCInstance(name: "ThirdVC")]
- }()
- override func viewDidLoad() {
- super.viewDidLoad()
- self.dataSource = self
- self.delegate = self
- if let firstVC = VCArr.first{
- setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
- }
- // Do any additional setup after loading the view.
- }
- private func VCInstance(name:String) -> UIViewController{
- return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
- }
- func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
- guard let viewControllerIndex = VCArr.firstIndex(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]
- }
- func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
- guard let viewControllerIndex = VCArr.firstIndex(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]
- }
- func presentationCount(for pageViewController: UIPageViewController) -> Int {
- return VCArr.count
- }
- func presentationIndex(for pageViewController: UIPageViewController) -> Int {
- guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArr.firstIndex(of: firstViewController) else{
- return 0
- }
- return firstViewControllerIndex
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.
- // Pass the selected object to the new view controller.
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement