Created
June 16, 2017 10:04
-
-
Save christianascone/796d8a433384da2e810b2d59eb039008 to your computer and use it in GitHub Desktop.
UIViewController extension with functions for showing OnBoarding and setting as 'done' using UserDefaults
This file contains 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
// | |
// UIViewController+Extension.swift | |
// | |
// Created by Christian Ascone on 16/06/17. | |
// | |
import Foundation | |
import KVNProgress | |
import Onboard // https://github.com/mamaral/Onboard | |
extension UIViewController { | |
/// Check if the onboard has been already done | |
func isOnboardDone() -> Bool{ | |
return UserDefaults.standard.bool(forKey: Constants.ONBOARDING_DONE) | |
} | |
/// Present the onboarding tutorial | |
func presentOnboardTutorial() { | |
var onboardingVC = OnboardingViewController() | |
let firstPage = OnboardingContentViewController(title: nil, body: nil, image: UIImage(named: "pag1"), buttonText: nil) { () -> Void in | |
// do something here when users press the button, like ask for location services permissions, register for push notifications, connect to social media, or finish the onboarding process | |
} | |
let secondPage = OnboardingContentViewController(title: nil, body: nil, image: UIImage(named: "pag2"), buttonText: nil) { () -> Void in | |
// do something here when users press the button, like ask for location services permissions, register for push notifications, connect to social media, or finish the onboarding process | |
} | |
let thirdPage = OnboardingContentViewController(title: nil, body: nil, image: UIImage(named: "pag3"), buttonText: nil) { () -> Void in | |
// do something here when users press the button, like ask for location services permissions, register for push notifications, connect to social media, or finish the onboarding process | |
} | |
let fourthPage = OnboardingContentViewController(title: nil, body: nil, image: UIImage(named: "pag4"), buttonText: nil) { () -> Void in | |
// do something here when users press the button, like ask for location services permissions, register for push notifications, connect to social media, or finish the onboarding process | |
} | |
let fifthPage = OnboardingContentViewController(title: nil, body: nil, image: UIImage(named: "pag5"), buttonText: "Let's go") { () -> Void in | |
UserDefaults.standard.set(true, forKey: Constants.ONBOARDING_DONE) | |
UserDefaults.standard.synchronize() | |
onboardingVC.dismiss(animated: true, completion: nil) | |
} | |
let contentControllers = [firstPage, secondPage, thirdPage, fourthPage, fifthPage] | |
for page in contentControllers { | |
page.topPadding = 100 | |
let aspectRatio = (page.iconImageView.image?.size.width)! / (page.iconImageView.image?.size.height)! | |
page.iconWidth = page.view.layer.bounds.width | |
page.iconHeight = page.iconWidth / aspectRatio | |
} | |
// Image | |
onboardingVC = OnboardingViewController(backgroundImage: UIImage(named: "onboard_background"), contents: contentControllers) | |
self.present(onboardingVC, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment