娛樂菜單展示
效果展示
- 如圖
思路分析
- 該界面其實非常常見,像微信聊天中就有類似的界面
- 該界面如何佈局呢?
思路一: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
fileprivate lazy var menuView : AmuseMenuView = {
let menuView = AmuseMenuView.amuseMenuView()
menuView.frame = CGRect(x: 0, y: -kMenuViewH, width: kScreenW, height: kMenuViewH)
return menuView
}()
- 添加到UICollectionView中,並且設置UICollectionView的內邊距
override func setupUI() {
super.setupUI()
collectionView.addSubview(menuView)
collectionView.contentInset = UIEdgeInsets(top: kMenuViewH, left: 0, bottom: 0, right: 0)
}
- 實現UICollectionView的數據源&代理
- 實現數據源方法,展示數據
- 註冊Cell(在awakeFromNib中註冊)
- 設置佈局(在layoutSubviews中設置,該位置可以拿到正確的尺寸)
import UIKit
private let kMenuCellID = "kMenuCellID"
class AmuseMenuView: UIView {
// MARK: 控件屬性
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
// MARK: 從Xib中加載出來
override func awakeFromNib() {
super.awakeFromNib()
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kMenuCellID)
}
override func layoutSubviews() {
super.layoutSubviews()
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = collectionView.bounds.size
}
}
// MARK:- 提供快速創建的類方法
extension AmuseMenuView {
class func amuseMenuView() -> AmuseMenuView {
return Bundle.main.loadNibNamed("AmuseMenuView", owner: nil, options: nil)?.first as! AmuseMenuView
}
}
// MARK:- 遵守UICollectionView的數據源&代理協議
extension AmuseMenuView : UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// 1.取出Cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kMenuCellID, for: indexPath)
cell.backgroundColor = UIColor.randomColor()
return cell
}
}
- 自定義Cell,用於展示每一個Item
- 在Cell中添加UICollectionView
- 設置UICollectionView的數據源&代理
- 註冊Cell(在awakeFromNib中註冊)
- 設置佈局(在layoutSubviews中設置,該位置可以拿到正確的尺寸
import UIKit
private let kGameCellID = "kGameCellID"
class AmuseMenuViewCell: UICollectionViewCell {
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kGameCellID)
}
override func layoutSubviews() {
super.layoutSubviews()
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
let itemW = collectionView.bounds.width / 4
let itemH = collectionView.bounds.height / 2
layout.itemSize = CGSize(width: itemW, height: itemH)
}
}
extension AmuseMenuViewCell : UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGameCellID, for: indexPath)
cell.backgroundColor = UIColor.randomColor()
return cell
}
}
- 展示數據
- AmuseMenuView定義[GameBaseModel]屬性
- 監聽屬性的改變,將數據每8個傳遞給Cell一次
- Cell拿到數據,將數據展示到GameCell中即可
- 代碼叫簡單(此處不再粘貼)
來源:http://bbs.520it.com/forum.php?mod=viewthread&tid=2499