啓動引導頁

//

//  AppDelegate.swift

//  FlowerField

//

//  Created by CJW on 17/6/5.

//  Copyright © 2017 cjw. All rights reserved.

//


import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?



    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        

        // 設置全局的UINavigationBars屬性

        let bar = UINavigationBar.appearance()

        bar.tintColor = UIColor.redColor()

        bar.titleTextAttributes = [NSFontAttributeName:UIFont.systemFontOfSize(15),NSForegroundColorAttributeName:UIColor.blackColor()]

        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        window?.backgroundColor = UIColor.whiteColor()

        // 根據版本號,判斷顯示哪個控制器

        if toNewFeature() {

            window?.rootViewController = NewFeatureViewController()

        }else {

            window?.rootViewController = MainViewController()

        }

        window?.makeKeyAndVisible()

        

        return true

    }

     private let XCBundleShortVersionString = "XCBundleShortVersionString"

    // MARK: - 判斷版號

    private func toNewFeature() -> Bool {

        // 根據版本號來確定是否進入新特性界面

        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String

        let oldVersion = NSUserDefaults.standardUserDefaults().objectForKey(XCBundleShortVersionString) ?? ""

         // 如果當前的版本號和本地保存的版本比較是降序, 則需要顯示新特性

        if (currentVersion.compare(oldVersion as! String)) == .OrderedDescending{

            //保存當前版本

            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: XCBundleShortVersionString)

            return true

        }

        return false

    }

}


//

//  NewFeatureViewController.swift

//  FlowerField

//

//  Created by CJW on 17/6/5.

//  Copyright © 2017 cjw. All rights reserved.

//


import UIKit

import SnapKit


private let reuseIdentifier = "reuseIdentifier"


class NewFeatureViewController: UICollectionViewController {


    //MARK: - 生命週期方法

    init() {

        super.init(collectionViewLayout:newFeatureFlowLayout())

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

          setup()

    }

    private func setup() {

          //註冊CELL

          self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

          // 添加pageControll

         collectionView?.addSubview(pageControll)

        

        // 佈局

        pageControll.snp_makeConstraints { (make) in

            make.bottom.equalTo(self.view).offset(-10)

            make.centerX.equalTo(self.view)

            make.size.equalTo(CGSize(width: 100, height: 20))

        }

  

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        // #warning Incomplete implementation, return the number of items

        return imageNames.count ?? 0

    }


    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell:NewFeatureCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell

    

        let count = imageNames.count ?? 0

        if count > 0 {

            cell.image = UIImage(named: imageNames[indexPath.item])

        }

        // Configure the cell

    

        return cell

    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        // 能進入這兒,imageNames.count肯定有值了

        if indexPath.item == imageNames.count - 1 {

            keyWindow.rootViewController = MainViewController()

        }

    }

    

    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

        let currendPage = Int(scrollView.contentOffset.x / self.view.bounds.width + 0.5)

        //最後一頁不顯示pageControll

        pageControll.currentPage = currendPage

    }

    override func scrollViewDidScroll(scrollView: UIScrollView) {

        if scrollView.contentOffset.x/self.view.bounds.width > (CGFloat(imageNames.count)-1.5){

            pageControll.hidden = true

        }else {

            pageControll.hidden = false

        }

    }

    

    

    // MARK: -懶加載

    /// 新特性的圖片數組

    private let imageNames:[String] = ["gp_bg_0","gp_bg_1", "gp_bg_2"]

    /// pageControll

    private lazy var pageControll:UIPageControl = {

        let pageContr = UIPageControl()

        pageContr.numberOfPages = self.imageNames.count ?? 0

        pageContr.pageIndicatorTintColor = UIColor.whiteColor()

        pageContr.currentPageIndicatorTintColor = UIColor.yellowColor()

        return pageContr

    }()


}


class newFeatureFlowLayout: UICollectionViewFlowLayout {

    override func prepareLayout() {

        super.prepareLayout()

        // MARK: 設置layout

        // 設置itemSize

        itemSize = UIScreen.mainScreen().bounds.size

        //設置間距

        minimumLineSpacing = 0

        minimumInteritemSpacing = 0

        //設置方向

        scrollDirection = .Horizontal

        

        // MARK: 設置collectionVIew

        // 設置分頁

        collectionView?.pagingEnabled = true

        // 設置隱藏橫豎的滾動條

        collectionView?.showsVerticalScrollIndicator = false

        collectionView?.showsHorizontalScrollIndicator = false

        //取消彈簧效果

        collectionView?.bounces = false

    }

}



//

//  NewFeatureCell.swift

//  FlowerField

//

//  Created by CJW on 17/6/5.

//  Copyright © 2017 cjw. All rights reserved.

//


import UIKit

import SnapKit


class NewFeatureCell: UICollectionViewCell {

    //顯示的image

    var image:UIImage?{

        didSet {

            if let img = image {

                imageView.image = img

            }

        }

    }

    

    // MARK : - 內部控制方法

    override init(frame: CGRect) {

        super.init(frame:frame)

        setup()

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    private func setup(){

        contentView.addSubview(imageView)

        imageView.snp_makeConstraints { (make) in

            make.edges.equalTo(0)

        }

    }

    

    // MARK: - 懶加載

    private lazy var imageView:UIImageView = UIImageView()


}

















發佈了145 篇原創文章 · 獲贊 32 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章