应用"新特性"功能demo

1.利用UICollectionViewController来做应用新特性的界面

//
//  NewFeatureCollectionViewController.swift

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionViewController: UICollectionViewController {
    /// 页面个数
    private let pageCount = 4;
    /// 布局对象
    private var layout:UICollectionViewFlowLayout = NewFeatureLayout()

    init(){
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // 1.注册cell
        // UICollectionViewCell.self 在OC里这样写:[UICollectionViewCell class]
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource
    /**
     返回一个有多少个cell
     */
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCount
    }

    /**
     返回对应indexPath的cell
     */
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

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

        // 2.设置cell上的数据
        cell.imageIndex = indexPath.item

        // 3.返回cell
        return cell
    }

    // MARK: UICollectionViewDelegate

    /**
     一个cell完成显示后会调用
     */
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

        //indexPath 是上一页的索引

        // 1.拿到当前显示的cell对应的索引
        let path = collectionView.indexPathsForVisibleItems().last!

        // 2.拿到对应的cell,让cell来执行按钮动画
        if path.item == (pageCount - 1) {
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            cell.startBtnAnimation()
        }
    }


}

/// 自定义的UICollectionViewCell
class NewFeatureCell:UICollectionViewCell{

    /// 保存图片的索引
    var imageIndex:Int? {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }

    /**
     让按钮做动画
     */
    func startBtnAnimation(){
        startButton.hidden = false
        // 执行动画
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startButton.userInteractionEnabled = false

        // UIViewAnimationOptions(rawValue: 0) 对应OC的 knilOptions
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: {
            // 清空形变
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: { (_) in
                self.startButton.userInteractionEnabled = true
        })
    }

    /**
     『开始微博』按钮被点击了
     */
    func startBtnClick(){
        // 去主页
        // 注意点:企业开发中如果要切换根控制器,最好都在AppDelegate中切换
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerKey, object: true)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        // 1.初始化UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){
         // 1.添加子控件到contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)

        // 2.布局子控件
        iconView.xmg_Fill(contentView)
        startButton.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil,offset: CGPoint(x: 0,y: -160))
    }

    // MARK: - 懒加载
    private lazy var iconView = UIImageView()
    private lazy var startButton:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_Highlighted"), forState: UIControlState.Highlighted)
        btn.hidden = true

        btn.addTarget(self, action: #selector(NewFeatureCell.startBtnClick), forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()
}

/// 自定义布局
private class NewFeatureLayout:UICollectionViewFlowLayout{

    /**
     自定义布局,要重写这个方法
     */
    override func prepareLayout() {
        // 准备布局
        // 会先调用一共有多少行的方法,再调用返回每一个的cell


        // 1.设置layout布局
        itemSize = UIScreen.mainScreen().bounds.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal


        // 2.设置collectionView的属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }
}

2.接下来需要在AppDelegate里判断是否是新版本?

    /**
     判断有木有新版本
     */
    private func isNewUpdate() -> Bool {
        // 1.获取当前软件的版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String

        // 2.获取之前的软件版本号(存在本地)
        let sanboxVersion = NSUserDefaults.standardUserDefaults().objectForKey("CFBundleShortVersionString") as? String ?? ""

        // 3.比较
        if currentVersion.compare(sanboxVersion) == NSComparisonResult.OrderedDescending {
            // 有新版本
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "CFBundleShortVersionString")

            return true
        }

        return false
    }

3.如果是新版本,就设置窗口根控制器是新版本控制器

/**
     用于获取默认界面
     */
    private func defaultController() -> UIViewController {
        if isNewUpdate() {
            return NewFeatureCollectionViewController()
        }

        return MainViewController()
    }

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)

 // 1.创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()

        // 2.创建根控制器
        window?.rootViewController = defaultController()
        window?.makeKeyAndVisible()

        return true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值