Skip to content

Instantly share code, notes, and snippets.

@Mr-Perfection
Created September 2, 2019 02:27
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 Mr-Perfection/184bd82dcfe2eb831c413ea5689f9295 to your computer and use it in GitHub Desktop.
Save Mr-Perfection/184bd82dcfe2eb831c413ea5689f9295 to your computer and use it in GitHub Desktop.
//
// OnboardingViewController.swift
// PWMe
//
// Created by Stephen lee on 8/22/19.
// Copyright © 2019 Stephen lee. All rights reserved.
//
import UIKit
class OnboardingViewController: UIViewController, UIPageViewControllerDataSource {
static var storyboardID : String {
return "OnboardingStoryboardID"
}
weak var pvc : UIPageViewController!
override func viewDidLoad() {
super.viewDidLoad()
self.pvc = self.children[0] as? UIPageViewController
self.pvc.dataSource = self
let p = SingleInputPage(num: 1, total: ONBOARDING_COUNT)
pvc.setViewControllers([p], direction: .forward, animated: false, completion: nil)
self.removeSwipeGesture()
}
private func removeSwipeGesture(){
for view in self.pvc!.view.subviews {
if let subView = view as? UIScrollView {
subView.isScrollEnabled = false
}
}
}
private func getPageClass() -> Page? {
let vc = self.pvc.viewControllers![0]
// SetUsername page
if let current = vc as? SingleInputPage {
return current
}
// SetSkillLevels page
if let current = vc as? SkillLevelsPage {
return current
}
return nil
}
func pageViewController(_ p: UIPageViewController, viewControllerAfter vc: UIViewController) -> UIViewController? {
if let cur = getPageClass() {
let num = cur.num
switch num {
case 1:
return SkillLevelsPage(num: num+1, total: ONBOARDING_COUNT)
case ONBOARDING_COUNT:
return AppStoryboard.Main.instance.instantiateInitialViewController()
default:
print("Unexpected behavior in viewControllerAfter. num:\(num) ONBOARDING_COUNT \(ONBOARDING_COUNT)")
return SingleInputPage(num: num+1, total: ONBOARDING_COUNT)
}
}
return nil
}
func pageViewController(_ p: UIPageViewController, viewControllerBefore vc: UIViewController) -> UIViewController? {
if let cur = getPageClass() {
let num = cur.num
if num == 1 {
return nil
}
return SingleInputPage(num: num-1, total: ONBOARDING_COUNT)
}
return nil
}
@objc func goNextPage(_:AnyObject) {
if let current = getPageClass() {
current.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
current.view.isUserInteractionEnabled = true
let p = self.pageViewController(self.pvc, viewControllerAfter: current)
self.pvc.setViewControllers([p!], direction: .forward, animated: true, completion: nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment