Skip to content

Instantly share code, notes, and snippets.

@onevcat
Created September 12, 2019 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onevcat/fbf71a2dfce2533ec563a14a0d009e0d to your computer and use it in GitHub Desktop.
Save onevcat/fbf71a2dfce2533ec563a14a0d009e0d to your computer and use it in GitHub Desktop.
final class ViewController: UIPageViewController {
class Inner: UIViewController {
var color: UIColor!
var name: String!
override func viewDidLoad() {
view.backgroundColor = color
}
override var description: String {
return name
}
}
private var controllers: [UIViewController] = []
override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
super.init(
transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: options
)
}
required init?(coder: NSCoder) {
super.init(
transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: nil
)
}
override func viewDidLoad() {
super.viewDidLoad()
let colors: [(UIColor, String)] = [(.red, "R"), (.green, "G"), (.blue, "B")]
controllers = colors.map { color in
let c = Inner()
c.color = color.0
c.name = color.1
return c
}
setViewControllers([controllers[0]],
direction: .forward,
animated: false,
completion: nil)
dataSource = self
delegate = self
}
}
extension ViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
print(pageViewController.viewControllers!.first)
}
}
extension ViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let index = controllers.firstIndex(of: viewController),
index > 0 {
return controllers[index-1]
} else {
return nil
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = controllers.firstIndex(of: viewController),
index < controllers.count-1 {
return controllers[index+1]
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment