- 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
}
}
网友评论