Last active
November 16, 2020 12:33
-
-
Save mkerley/6f55ab5ec9c5c23845cdd65061a42932 to your computer and use it in GitHub Desktop.
Sample code for a UIPageViewController implementation. Two versions, one with wraparound between first and last page, and one without.
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
// | |
// MyPageViewController.swift | |
// PageViewExperiment | |
// | |
// Adapted from http://samwize.com/2015/10/13/how-to-create-uipageviewcontroller-in-storyboard-in-container-view/ | |
// Updated for Swift 3 and (IMHO) minor code style improvements, and modified page turning behavior to not wrap around | |
// | |
import UIKit | |
class MyPageViewController: UIPageViewController { | |
var pages = [UIViewController]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
dataSource = self | |
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page1") | |
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page2") | |
pages.append(page1) | |
pages.append(page2) | |
setViewControllers([page1], direction: .forward, animated: false, completion: nil) | |
} | |
} | |
extension MyPageViewController: UIPageViewControllerDataSource { | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
let currentIndex = pages.index(of: viewController)! | |
// This version will not allow pages to wrap around | |
guard currentIndex > 0 else { | |
return nil | |
} | |
return pages[currentIndex - 1] | |
} | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
let currentIndex = pages.index(of: viewController)! | |
// This version will not allow pages to wrap around | |
guard currentIndex < pages.count - 1 else { | |
return nil | |
} | |
return pages[currentIndex + 1] | |
} | |
func presentationCount(for pageViewController: UIPageViewController) -> Int { | |
return 2 | |
} | |
func presentationIndex(for pageViewController: UIPageViewController) -> Int { | |
return 0 | |
} | |
} |
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
// | |
// MyPageViewController.swift | |
// PageViewExperiment | |
// | |
// Adapted from http://samwize.com/2015/10/13/how-to-create-uipageviewcontroller-in-storyboard-in-container-view/ | |
// Updated for Swift 3 and (IMHO) minor code style improvements | |
// | |
import UIKit | |
class MyPageViewController: UIPageViewController { | |
var pages = [UIViewController]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
dataSource = self | |
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page1") | |
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page2") | |
pages.append(page1) | |
pages.append(page2) | |
setViewControllers([page1], direction: .forward, animated: false, completion: nil) | |
} | |
} | |
extension MyPageViewController: UIPageViewControllerDataSource { | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
let currentIndex = pages.index(of: viewController)! | |
// This math will make the pages wrap around | |
let previousIndex = abs((currentIndex - 1) % pages.count) | |
return pages[previousIndex] | |
} | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
let currentIndex = pages.index(of: viewController)! | |
// This math will make the pages wrap around | |
let nextIndex = abs((currentIndex + 1) % pages.count) | |
return pages[nextIndex] | |
} | |
func presentationCount(for pageViewController: UIPageViewController) -> Int { | |
return 2 | |
} | |
func presentationIndex(for pageViewController: UIPageViewController) -> Int { | |
return 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work reliably for me:
I have 4 pages.
When I start on page 1 and immediately scroll back, it goes to page 2.
When I start on page 1 and scroll forward through page 4, it wraps to page 1, then I can scroll back to page 4.
When I start on page 1 and scroll forward through page 4, it wraps to page 1, then scroll to page 2 and then go backwards it loops back to page 2 instead of page 4.