使用SnapKit作应用新手上路导览 发表于 2016-09-14 | 分类于 前端 | 具体实现:YMNewfeatureViewController.swift123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import UIKitimport SnapKitlet newFeatureID = "newFeatureID"class YMNewfeatureViewController: UICollectionViewController { /// 布局对象 private var layout: UICollectionViewFlowLayout = YMNewfeatureLayout() init() { super.init(collectionViewLayout: layout) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() collectionView?.registerClass(YMNewfeatureCell.self, forCellWithReuseIdentifier: newFeatureID) } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return kNewFeatureCount } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(newFeatureID, forIndexPath: indexPath) as! YMNewfeatureCell cell.imageIndex = indexPath.item return cell } // 完全显示一个cell之后调用 override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { let path = collectionView.indexPathsForVisibleItems().last! if path.item == (kNewFeatureCount - 1) { let cell = collectionView.cellForItemAtIndexPath(path) as! YMNewfeatureCell cell.startBtnAnimation() } }}/// YMNewfeatureCellprivate class YMNewfeatureCell: UICollectionViewCell { private var imageIndex: Int? { didSet { iconView.image = UIImage(named: "walkthrough_\(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, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in // 清空形变 self.startButton.transform = CGAffineTransformIdentity }, completion: { (_) -> Void in self.startButton.userInteractionEnabled = true }) } override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupUI() { contentView.addSubview(iconView) contentView.addSubview(startButton) iconView.snp_makeConstraints { (make) in make.edges.equalTo(contentView) } startButton.snp_makeConstraints { (make) in make.bottom.equalTo(contentView.snp_bottom).offset(-50) make.size.equalTo(CGSizeMake(150, 40)) make.centerX.equalTo(0) } } private lazy var iconView = UIImageView() private lazy var startButton: UIButton = { let btn = UIButton() btn.setBackgroundImage(UIImage(named: "btn_begin"), forState: .Normal) btn.addTarget(self, action: #selector(startButtonClick), forControlEvents: .TouchUpInside) btn.layer.masksToBounds = true btn.hidden = true return btn }() @objc func startButtonClick() { UIApplication.sharedApplication().keyWindow?.rootViewController = YMTabBarController() }}private class YMNewfeatureLayout: UICollectionViewFlowLayout { /// 准备布局 private override func prepareLayout() { // 设置 layout 布局 itemSize = UIScreen.mainScreen().bounds.size minimumLineSpacing = 0 minimumInteritemSpacing = 0 scrollDirection = .Horizontal // 设置 contentView 属性 collectionView?.showsVerticalScrollIndicator = false collectionView?.bounces = false collectionView?.pagingEnabled = true }} 用户首次装载时应用:123456if !NSUserDefaults.standardUserDefaults().boolForKey(YMFirstLaunch) { window?.rootViewController = YMNewfeatureViewController() NSUserDefaults.standardUserDefaults().setBool(true, forKey: YMFirstLaunch)} else { window?.rootViewController = YMTabBarController()}