class HomeViewController: UIViewController {
private var pageViewController: UIPageViewController!
private lazy var viewControllers: [UIViewController] = {
var viewControllers = [UIViewController]()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstIntroViewController = storyboard.instantiateViewControllerWithIdentifier("intro1")
let secondIntroViewController = storyboard.instantiateViewControllerWithIdentifier("intro2")
viewControllers.append(firstIntroViewController)
viewControllers.append(secondIntroViewController)
return viewControllers
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc = segue.destinationViewController as? UIPageViewController {
pageViewController = vc
pageViewController.dataSource = self
pageViewController.delegate = self
pageViewController.setViewControllers([viewControllers[0]], direction: .Forward, animated: true, completion: nil)
}
}
}
// MARK: UIPageViewControllerDataSource
extension HomeViewController: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = viewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard viewControllers.count > previousIndex else {
return nil
}
return viewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = viewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let viewControllersCount = viewControllers.count
guard viewControllersCount != nextIndex else {
return nil
}
guard viewControllersCount > nextIndex else {
return nil
}
return viewControllers[nextIndex]
}
}
// MARK: UIPageViewControllerDelegate
extension HomeViewController: UIPageViewControllerDelegate {
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return viewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.