當(dāng)程序被打開時,在創(chuàng)建KeyWindow的RootViewController時判斷是否是首次登陸,這里的邏輯是如果用戶是首次打開應(yīng)用的話顯示引導(dǎo)頁,當(dāng)點擊引導(dǎo)頁最后一頁的立即體驗時直接進(jìn)入主頁
在AppDelegate.swift
中加入以下代碼:這里我使用的是第一種方法
,這里沒有貼出效果圖,大家只要把代碼粘貼到項目中,并且替換圖片,即可運行:
第一種方法:
private func buildKeyWindow() {
window = UIWindow(frame: ScreenBounds)
window!.makeKeyAndVisible()
let isFristOpen = NSUserDefaults.standardUserDefaults().objectForKey("isFristOpenApp")
if isFristOpen == nil {
window?.rootViewController = GuideViewController()
NSUserDefaults.standardUserDefaults().setObject("isFristOpenApp", forKey: "isFristOpenApp")
} else {
window!.rootViewController = ViewController()
}
}
第二種方法:
//MARK: - 引導(dǎo)頁設(shè)置
private func showLeadpage() -> UIViewController{
let versionStr = "CFBundleShortVersionString"
let cureentVersion = NSBundle.mainBundle().infoDictionary![versionStr] as! String
let oldVersion = (NSUserDefaults.standardUserDefaults().objectForKey(versionStr) as? String) ?? ""
if cureentVersion.compare(oldVersion) == NSComparisonResult.OrderedAscending {
NSUserDefaults.standardUserDefaults().setObject(cureentVersion, forKey: versionStr)
NSUserDefaults.standardUserDefaults().synchronize()
return GuideViewController()
}
return ViewController()
}
在GuideViewController
中,我們使用UICollectionView
來轉(zhuǎn)載我們的引導(dǎo)頁:
import UIKit
public let ScreenWidth: CGFloat = UIScreen.mainScreen().bounds.size.width
public let ScreenHeight: CGFloat = UIScreen.mainScreen().bounds.size.height
public let ScreenBounds: CGRect = UIScreen.mainScreen().bounds
class GuideViewController: UIViewController {
private var collectView: UICollectionView?
private var imageNames = ["guide_40_1", "guide_40_2", "guide_40_3", "guide_40_4"]
private let cellIdentifier = "GuideCell"
private var isHiddenNextButton = true
private var pageController = UIPageControl(frame: CGRectMake(0, ScreenHeight - 50, ScreenWidth, 20))
override func viewDidLoad() {
super.viewDidLoad()
buildCollectionView()
buildPageController()
}
// MARK: - Build UI
private func buildCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.itemSize = ScreenBounds.size
layout.scrollDirection = .Horizontal
collectView = UICollectionView(frame: ScreenBounds, collectionViewLayout: layout)
collectView?.delegate = self
collectView?.dataSource = self
collectView?.showsVerticalScrollIndicator = false
collectView?.showsHorizontalScrollIndicator = false
collectView?.pagingEnabled = true
collectView?.bounces = false
collectView?.registerClass(GuideCell.self, forCellWithReuseIdentifier: cellIdentifier)
view.addSubview(collectView!)
}
func buildPageController() {
pageController.numberOfPages = imageNames.count
pageController.currentPage = 0
view.addSubview(pageController)
}
}
extension GuideViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageNames.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! GuideCell
cell.newImage = UIImage(named: imageNames[indexPath.row])
if indexPath.row != imageNames.count - 1 { // 3
cell.setNextButtonHidden(true) // 如果不是第三張就隱藏button
}
return cell
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView.contentOffset.x == ScreenWidth * CGFloat(imageNames.count - 1) {
let cell = collectView!.cellForItemAtIndexPath(NSIndexPath(forRow: imageNames.count - 1, inSection: 0)) as! GuideCell
cell.setNextButtonHidden(false)
isHiddenNextButton = false
}
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.x != ScreenWidth * CGFloat(imageNames.count - 1) && !isHiddenNextButton && scrollView.contentOffset.x > ScreenWidth * CGFloat(imageNames.count - 2) {
let cell = collectView!.cellForItemAtIndexPath(NSIndexPath(forRow: imageNames.count - 1, inSection: 0)) as! GuideCell
cell.setNextButtonHidden(true)
isHiddenNextButton = true
}
pageController.currentPage = Int(scrollView.contentOffset.x / ScreenWidth + 0.5)
}
}
在GuideCell
中的代碼:
import UIKit
// 點擊Guide頁的button
public let GuideViewControllerDidFinish = "GuideViewControllerDidFinish"
class GuideCell: UICollectionViewCell {
private let newImageView = UIImageView(frame: ScreenBounds)
private let nextButton = UIButton(frame: CGRectMake((ScreenWidth - 100) * 0.5, ScreenHeight - 110, 100, 33))
var newImage: UIImage? {
didSet {
newImageView.image = newImage
}
}
override init(frame: CGRect) {
super.init(frame: frame)
newImageView.contentMode = .ScaleAspectFill
contentView.addSubview(newImageView)
nextButton.setBackgroundImage(UIImage(named: "icon_next"), forState: UIControlState.Normal)
nextButton.addTarget(self, action: #selector(GuideCell.nextButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
nextButton.hidden = true
contentView.addSubview(nextButton)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setNextButtonHidden(hidden: Bool) {
nextButton.hidden = hidden
}
// GuideViewControllerDidFinish 還有一處在app.delegate中 進(jìn)入到主界面中使用的
func nextButtonClick() {
NSNotificationCenter.defaultCenter().postNotificationName(GuideViewControllerDidFinish, object: nil)
}
}