- 1.用來做引導圖的控件可以根據自己的選擇來決定,我在此用的是 UICollectionView 做的
- 2.說明一下,swift的一個控制器里面可以創建多個類
- 3.NewFeatureCollectionViewController.swift具體的代碼如下
111.gif
import UIKit
private let reuseIdentifier = "Cell"
class NewFeatureCollectionViewController: UICollectionViewController {
private let pageCount = 3
private var layout: UICollectionViewFlowLayout = NewFeaturelayout()
// 因為系統初始化的構造方法是帶參數的(UICollectionViewFlowLayout) 而不是不帶參數的,所以不用加 override
init(){
super.init(collectionViewLayout: layout)
self.collectionView?.backgroundColor = UIColor.white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
/*
//1.設置layout的布局
layout.itemSize = UIScreen.main.bounds.size
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
// 2.設置collectionView的屬性
collectionView?.showsHorizontalScrollIndicator = false
collectionView?.bounces = false
collectionView?.isPagingEnabled = true
*/
}
override func viewDidLoad() {
super.viewDidLoad()
// 1.注冊一個cell
collectionView?.register(newFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: UICollectionViewDataSource UICollectionViewDelegate
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
// 返回一共有多少個cell
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pageCount
}
// cell的重寫
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// 1.獲取cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! newFeatureCell
// 2.設置cell的數據
cell.ImageIndex = indexPath.item + 1
// 3.返回cell
return cell
}
override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// 傳遞給我們的是上一頁的索引
let path = collectionView.indexPathsForVisibleItems.last
print(path!)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == pageCount-1 {
print("您點擊的是租后一頁,頁碼是第\(indexPath.item+1)頁")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// MARK: 自定義cell swift的一個文件里面可以定義多個類的
private class newFeatureCell: UICollectionViewCell
{
// 保存圖片的索引
// 在swift里面,private修飾的東西如果是在同一個文件下是可以被調用的
var ImageIndex:Int?{
didSet{
iconView.image = UIImage(named:"guide_\(ImageIndex!).jpg")
}
}
override init(frame: CGRect) {
super.init(frame: frame)
// 1.初始化UI
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI(){
// 1.添加子控件到 UICollectionView 上
contentView.addSubview(iconView)
// 2.布局子控件的位置
iconView.frame = contentView.frame
}
// MARK:- 懶加載
private lazy var iconView: UIImageView = {
let iconViewImage = UIImageView()
iconViewImage.contentMode = UIViewContentMode.scaleAspectFill
iconViewImage.clipsToBounds = true
return iconViewImage
}()
// MARK: 重寫layout方法
private class NewFeaturelayout: UICollectionViewFlowLayout {
override func prepare() {
// 1.準備布局
// 什么時候調用? 1.先調用一個有 多少cell 2.調用準備布局,3.調用返回cell
itemSize = UIScreen.main.bounds.size
minimumLineSpacing = 0
minimumInteritemSpacing = 0
scrollDirection = UICollectionViewScrollDirection.horizontal
// 2.設置collectionView的屬性
collectionView?.showsHorizontalScrollIndicator = false
collectionView?.bounces = false
collectionView?.isPagingEnabled = true
}
}