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