小码哥-斗鱼直播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

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