swift微博第14天(新特性的引導圖)

  • 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
   }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,252評論 4 61
  • 前言 寫博客是一件很有意義的事情,可以加深自己對技術的理解,可以結交更多的朋友,記錄自己的技術軌跡,而且分享可以讓...
    foxleezh閱讀 6,482評論 7 34
  • 我開始憎恨人類......
    妖肆閱讀 193評論 0 1
  • 永靖縣公安局交警大隊 10月23日工作開展情況:交通安保實施方案,出動警力50人,出動警車6輛,查處各類交通違法行...
    隨風ABCD閱讀 138評論 0 0
  • 當我第一次接觸到這幾字的時候,我是一臉懵逼的,為什么呢? 這肯定是不對的呀,因為我們一般會認為,獎勵越多,動力就會...
    思緒的速度閱讀 690評論 0 0