使用SnapKit作应用新手上路导览

具体实现:
YMNewfeatureViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import UIKit
import SnapKit
let 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()
}
}
}
/// YMNewfeatureCell
private 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
}
}

用户首次装载时应用:

1
2
3
4
5
6
if !NSUserDefaults.standardUserDefaults().boolForKey(YMFirstLaunch) {
window?.rootViewController = YMNewfeatureViewController()
NSUserDefaults.standardUserDefaults().setBool(true, forKey: YMFirstLaunch)
} else {
window?.rootViewController = YMTabBarController()
}