遊戲界面實現
界面效果展示
- 界面效果
界面分析
- 分析圖
- 如何實現
- 這個界面實現的方案很多
- 可以直接使用UITableView,之後添加一個HeaderView
- 可以直接使用UICollectionView,之後添加內邊距(類似推薦界面實現)
- 這裏採取UICollectionView的方案
- 下面每一個遊戲就是一個Cell,如果使用UITableView則需要對數據進行進一步處理
- 另外上面部分,直接添加內邊距,並且添加內容即可
界面實現
添加UICollectionView
- 懶加載UICollectionView,並且設置相關屬性
fileprivate lazy var collectionView : UICollectionView = {
// 1.創建佈局
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: kItemW, height: kItemH)
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
layout.sectionInset = UIEdgeInsets(top: 0, left: kEdgeMargin, bottom: 0, right: kEdgeMargin)
// 2.創建UICollectionView
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kGameCellID)
collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
collectionView.backgroundColor = UIColor.white
collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
return collectionView
}()
// MARK:- 遵守UICollectionView的數據源&代理
extension GameViewController : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 60
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// 1.取出Cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGameCellID, for: indexPath) as! CollectionGameCell
return cell
}
}
接口描述
請求數據
- 創建對應的ViewModel,用於GameVc數據的請求
extension GameViewModel {
func loadAllGamesData(finishedCallback : @escaping () -> Void) {
NetworkTools.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/getColumnDetail?shortName=game") { (result) in
// 1.獲取數據
guard let resultDict = result as? [String : Any] else { return }
guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
// 2.字典轉模型
for dict in dataArray {
self.games.append(GameModel(dict: dict))
}
// 3.通知外界數據請求完成
finishedCallback()
}
}
}
// MARK:- 加載數據
extension GameViewController {
fileprivate func loadData() {
gameVM.loadAllGamesData {
self.collectionView.reloadData()
}
}
}
- 對之前的CollectionGameCell進行改進
// MARK: 定義模型屬性
var gameModel : GameBaseModel? {
didSet {
titleLabel.text = gameModel?.tag_name
if let iconURL = URL(string: gameModel?.icon_url ?? "") {
iconImageView.kf.setImage(with: iconURL, placeholder: UIImage(named: "home_more_btn"))
} else {
iconImageView.image = UIImage(named: "home_more_btn")
}
}
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
// 1.取出HeaderView
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath) as! CollectionHeaderView
headerView.titleLabel.text = "全部"
headerView.iconImageView.image = UIImage(named: "Img_orange")
headerView.moreBtn.isHidden = true
return headerView
}
添加內邊距,並且添加頂部的View
collectionView.contentInset = UIEdgeInsets(top: kHeaderViewH + kGameViewH, left: 0, bottom: 0, right: 0)
- 懶加載兩個View
- gameView:用於展示推薦遊戲
- headerView:頂部View的header
fileprivate lazy var gameView : RecommendGameView = {
let gameView = RecommendGameView.recommendGameView()
gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenW, height: kGameViewH)
return gameView
}()
fileprivate lazy var headerView : CollectionHeaderView = {
let headerView = CollectionHeaderView.collectionHeaderView()
headerView.frame = CGRect(x: 0, y: -(kHeaderViewH + kGameViewH), width: kScreenW, height: kHeaderViewH)
headerView.iconImageView.image = UIImage(named: "Img_orange")
headerView.titleLabel.text = "常用"
headerView.moreBtn.isHidden = true
return headerView
}()
fileprivate func setupUI() {
// 1.添加collectionView
view.addSubview(collectionView)
collectionView.contentInset = UIEdgeInsets(top: kHeaderViewH + kGameViewH, left: 0, bottom: 0, right: 0)
// 2.添加CommonView
// 2.1.添加headerView
collectionView.addSubview(headerView)
// 2.2.添加滾動的View
collectionView.addSubview(gameView)
}
來源:http://bbs.520it.com/forum.php?mod=viewthread&tid=2475