Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active February 16, 2017 14:41
Show Gist options
  • Save KentarouKanno/b5ea1d436b173790c79358b01f80828f to your computer and use it in GitHub Desktop.
Save KentarouKanno/b5ea1d436b173790c79358b01f80828f to your computer and use it in GitHub Desktop.
UIPageViewController

UIPageViewController

★ UIPageViewControllerを使用した場合
※ Infinite scroll

import UIKit

class PageViewController: UIPageViewController {
    
    lazy var vc1: ViewController1 = {
        let vc1 = self.storyboard!.instantiateViewController(withIdentifier: "vc1") as! ViewController1
        return vc1
    }()
    
    lazy var vc2: ViewController2 = {
        let vc2 = self.storyboard!.instantiateViewController(withIdentifier: "vc2") as! ViewController2
        return vc2
    }()
    
    lazy var vc3: ViewController3 = {
        let vc3 = self.storyboard!.instantiateViewController(withIdentifier: "vc3") as! ViewController3
        return vc3
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setViewControllers([vc1], direction: .forward, animated: true, completion: nil)
        self.dataSource = self
    }
}

extension PageViewController : UIPageViewControllerDataSource {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        switch viewController {
        case is ViewController3: return vc2 // 3 -> 2
        case is ViewController2: return vc1 // 2 -> 1
        case is ViewController1: return vc3 // 1 -> end of the road
        default: return nil
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        switch viewController {
        case is ViewController1: return vc2 // 1 -> 2
        case is ViewController2: return vc3 // 2 -> 3
        case is ViewController3: return vc1 // 3 -> end of the road
        default: return nil
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment