Created
September 12, 2019 05:08
-
-
Save onevcat/fbf71a2dfce2533ec563a14a0d009e0d to your computer and use it in GitHub Desktop.
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
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