App引導(dǎo)頁的實(shí)現(xiàn) (Swift純代碼)

當(dāng)程序被打開時(shí),在創(chuàng)建KeyWindow的RootViewController時(shí)判斷是否是首次登陸,這里的邏輯是如果用戶是首次打開應(yīng)用的話顯示引導(dǎo)頁,當(dāng)點(diǎn)擊引導(dǎo)頁最后一頁的立即體驗(yàn)時(shí)直接進(jìn)入主頁
AppDelegate.swift中加入以下代碼:這里我使用的是第一種方法,這里沒有貼出效果圖,大家只要把代碼粘貼到項(xiàng)目中,并且替換圖片,即可運(yùn)行:

第一種方法:



    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

// 點(diǎn)擊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)
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,752評(píng)論 1 14
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,835評(píng)論 4 61
  • 前言 這里使用 launchScreen 、.storyboard 文件創(chuàng)建啟動(dòng)圖和引導(dǎo)頁。首次打開項(xiàng)目或者更新后...
    四月_Hsu閱讀 7,557評(píng)論 2 10
  • iOS高仿愛鮮蜂 前言 2015年匆匆的就過去了,又老了一歲,這一年起起伏伏,有笑聲也有眼淚,感謝陪伴在我身邊的人...
    Top_熊閱讀 33,885評(píng)論 267 438
  • 每個(gè)人的路不同,使命也不同,一開始的時(shí)候,大家都一樣,讀書,工作,越走到后面,自我那個(gè)東西冒出尖尖的芽,等發(fā)芽生長...
    flyingfish2007閱讀 230評(píng)論 0 0

友情鏈接更多精彩內(nèi)容