小碼哥-鬥魚直播APP之娛樂菜單展示

娛樂菜單展示

效果展示

  • 如圖

思路分析

  • 該界面其實非常常見,像微信聊天中就有類似的界面
  • 該界面如何佈局呢?

思路一:UIScrollView

  • 整體是一個UIScrollView
    • 在UIScrollView中多頁View
    • 在每頁View中添加8個按鈕
  • 缺點:
    • 如果數據較多,則無法做到循環利用
    • 需要計算每一個按鈕的位置

思路二:UICollectionView

  • 整體是一個UICollectionView
    • 每一個按鈕是一個Cell
    • 滾動方向爲水平滾動(水平滾動時,則會從左到右依次排布)
  • 缺點:
    • 如果某業數據較少,排布方式會是上下排布
    • 而不是先排布第一排,後排布第二排
    • 如圖

思路三:UICollectionView的Cell中嵌套UICollectionView

  • 整體是一個UICollectionView
    • 一個Cell爲一頁
    • 一頁中防止一個UICollectionView
    • 該UICollectionView中放8個數據(如果達到8個,不足8個則水平依次排列)

界面實現

  • 自定義AmuseMenuView,用於描述整個頂部菜單
    • 該View中主要有一個UICollectionView,一個UIPageControl
    • 可以直接通過一個Xib來描述
  • 在控制器中加載該View,並且添加到UICollectionView中
  • 懶加載AmuseMenuView
  1. fileprivate lazy var menuView : AmuseMenuView = {
  2. let menuView = AmuseMenuView.amuseMenuView()
  3. menuView.frame = CGRect(x: 0, y: -kMenuViewH, width: kScreenW, height: kMenuViewH)
  4. return menuView
  5. }()
  • 添加到UICollectionView中,並且設置UICollectionView的內邊距
  1. override func setupUI() {
  2. super.setupUI()
  3. collectionView.addSubview(menuView)
  4. collectionView.contentInset = UIEdgeInsets(top: kMenuViewH, left: 0, bottom: 0, right: 0)
  5. }
  • 實現UICollectionView的數據源&代理
    • 實現數據源方法,展示數據
    • 註冊Cell(在awakeFromNib中註冊)
    • 設置佈局(在layoutSubviews中設置,該位置可以拿到正確的尺寸)
  1. import UIKit
  2. private let kMenuCellID = "kMenuCellID"
  3. class AmuseMenuView: UIView {
  4. // MARK: 控件屬性
  5. @IBOutlet weak var collectionView: UICollectionView!
  6. @IBOutlet weak var pageControl: UIPageControl!
  7. // MARK: 從Xib中加載出來
  8. override func awakeFromNib() {
  9. super.awakeFromNib()
  10. collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kMenuCellID)
  11. }
  12. override func layoutSubviews() {
  13. super.layoutSubviews()
  14. let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
  15. layout.itemSize = collectionView.bounds.size
  16. }
  17. }
  18. // MARK:- 提供快速創建的類方法
  19. extension AmuseMenuView {
  20. class func amuseMenuView() -> AmuseMenuView {
  21. return Bundle.main.loadNibNamed("AmuseMenuView", owner: nil, options: nil)?.first as! AmuseMenuView
  22. }
  23. }
  24. // MARK:- 遵守UICollectionView的數據源&代理協議
  25. extension AmuseMenuView : UICollectionViewDataSource, UICollectionViewDelegate {
  26. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  27. return 2
  28. }
  29. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  30. // 1.取出Cell
  31. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kMenuCellID, for: indexPath)
  32. cell.backgroundColor = UIColor.randomColor()
  33. return cell
  34. }
  35. }
  • 自定義Cell,用於展示每一個Item
    • 在Cell中添加UICollectionView
    • 設置UICollectionView的數據源&代理
    • 註冊Cell(在awakeFromNib中註冊)
    • 設置佈局(在layoutSubviews中設置,該位置可以拿到正確的尺寸
  1. import UIKit
  2. private let kGameCellID = "kGameCellID"
  3. class AmuseMenuViewCell: UICollectionViewCell {
  4. @IBOutlet weak var collectionView: UICollectionView!
  5. override func awakeFromNib() {
  6. super.awakeFromNib()
  7. collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kGameCellID)
  8. }
  9. override func layoutSubviews() {
  10. super.layoutSubviews()
  11. let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
  12. let itemW = collectionView.bounds.width / 4
  13. let itemH = collectionView.bounds.height / 2
  14. layout.itemSize = CGSize(width: itemW, height: itemH)
  15. }
  16. }
  17. extension AmuseMenuViewCell : UICollectionViewDataSource, UICollectionViewDelegate {
  18. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  19. return 8
  20. }
  21. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  22. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGameCellID, for: indexPath)
  23. cell.backgroundColor = UIColor.randomColor()
  24. return cell
  25. }
  26. }
  • 展示數據
    • AmuseMenuView定義[GameBaseModel]屬性
    • 監聽屬性的改變,將數據每8個傳遞給Cell一次
    • Cell拿到數據,將數據展示到GameCell中即可
    • 代碼叫簡單(此處不再粘貼)

    來源:http://bbs.520it.com/forum.php?mod=viewthread&tid=2499

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章