当程序被打开时,在创建KeyWindow的RootViewController时判断是否是首次登陆,这里的逻辑是如果用户是首次打开应用的话显示引导页,当点击引导页最后一页的立即体验时直接进入主页
在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: - 引导页设置
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
来转载我们的引导页:
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中 进入到主界面中使用的
func nextButtonClick() {
NSNotificationCenter.defaultCenter().postNotificationName(GuideViewControllerDidFinish, object: nil)
}
}