玩转【斗鱼直播APP】系列之游戏界面实现

游戏界面实现

界面效果展示

  • 界面效果

界面分析

  • 分析图
  • 如何实现
    • 这个界面实现的方案很多
    • 可以直接使用UITableView,之后添加一个HeaderView
    • 可以直接使用UICollectionView,之后添加内边距(类似推荐界面实现)
  • 这里采取UICollectionView的方案
    • 下面每一个游戏就是一个Cell,如果使用UITableView则需要对数据进行进一步处理
    • 另外上面部分,直接添加内边距,并且添加内容即可

界面实现

添加UICollectionView
  • 懒加载UICollectionView,并且设置相关属性
 
 
  1. fileprivate lazy var collectionView : UICollectionView = {
  2. // 1.创建布局
  3. let layout = UICollectionViewFlowLayout()
  4. layout.itemSize = CGSize(width: kItemW, height: kItemH)
  5. layout.minimumLineSpacing = 0
  6. layout.minimumInteritemSpacing = 0
  7. layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
  8. layout.sectionInset = UIEdgeInsets(top: 0, left: kEdgeMargin, bottom: 0, right: kEdgeMargin)
  9. // 2.创建UICollectionView
  10. let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
  11. collectionView.dataSource = self
  12. collectionView.delegate = self
  13. collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kGameCellID)
  14. collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
  15. collectionView.backgroundColor = UIColor.white
  16. collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  17. return collectionView
  18. }()
  • 设置数据源,注册Cell,实现数据源方法
 
 
  1. // MARK:- 遵守UICollectionView的数据源&代理
  2. extension GameViewController : UICollectionViewDataSource {
  3. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  4. return 60
  5. }
  6. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  7. // 1.取出Cell
  8. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGameCellID, for: indexPath) as! CollectionGameCell
  9. return cell
  10. }
  11. }
接口描述
参数名称参数说明
shortNamegame
请求数据
  • 创建对应的ViewModel,用于GameVc数据的请求
 
 
  1. extension GameViewModel {
  2. func loadAllGamesData(finishedCallback : @escaping () -> Void) {
  3. NetworkTools.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/getColumnDetail?shortName=game") { (result) in
  4. // 1.获取数据
  5. guard let resultDict = result as? [String : Any] else { return }
  6. guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
  7. // 2.字典转模型
  8. for dict in dataArray {
  9. self.games.append(GameModel(dict: dict))
  10. }
  11. // 3.通知外界数据请求完成
  12. finishedCallback()
  13. }
  14. }
  15. }
  • 发送网络请求
 
 
  1. // MARK:- 加载数据
  2. extension GameViewController {
  3. fileprivate func loadData() {
  4. gameVM.loadAllGamesData {
  5. self.collectionView.reloadData()
  6. }
  7. }
  8. }
  • 对之前的CollectionGameCell进行改进
    • 添加底部的线段
    • 展示数据即可
 
 
  1. // MARK: 定义模型属性
  2. var gameModel : GameBaseModel? {
  3. didSet {
  4. titleLabel.text = gameModel?.tag_name
  5. if let iconURL = URL(string: gameModel?.icon_url ?? "") {
  6. iconImageView.kf.setImage(with: iconURL, placeholder: UIImage(named: "home_more_btn"))
  7. } else {
  8. iconImageView.image = UIImage(named: "home_more_btn")
  9. }
  10. }
  11. }
  • 添加组的HeaderView
    • 实现对应的数据源方法即可
 
 
  1. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  2. // 1.取出HeaderView
  3. let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath) as! CollectionHeaderView
  4. headerView.titleLabel.text = "全部"
  5. headerView.iconImageView.image = UIImage(named: "Img_orange")
  6. headerView.moreBtn.isHidden = true
  7. return headerView
  8. }
添加内边距,并且添加顶部的View
  • 添加内边距
 
 
  1. collectionView.contentInset = UIEdgeInsets(top: kHeaderViewH + kGameViewH, left: 0, bottom: 0, right: 0)
  • 懒加载两个View
    • gameView:用于展示推荐游戏
    • headerView:顶部View的header
 
 
  1. fileprivate lazy var gameView : RecommendGameView = {
  2. let gameView = RecommendGameView.recommendGameView()
  3. gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenW, height: kGameViewH)
  4. return gameView
  5. }()
  6. fileprivate lazy var headerView : CollectionHeaderView = {
  7. let headerView = CollectionHeaderView.collectionHeaderView()
  8. headerView.frame = CGRect(x: 0, y: -(kHeaderViewH + kGameViewH), width: kScreenW, height: kHeaderViewH)
  9. headerView.iconImageView.image = UIImage(named: "Img_orange")
  10. headerView.titleLabel.text = "常用"
  11. headerView.moreBtn.isHidden = true
  12. return headerView
  13. }()
  • 在设置UI处添加两个View
 
 
  1. fileprivate func setupUI() {
  2. // 1.添加collectionView
  3. view.addSubview(collectionView)
  4. collectionView.contentInset = UIEdgeInsets(top: kHeaderViewH + kGameViewH, left: 0, bottom: 0, right: 0)
  5. // 2.添加CommonView
  6. // 2.1.添加headerView
  7. collectionView.addSubview(headerView)
  8. // 2.2.添加滚动的View
  9. collectionView.addSubview(gameView)
  10. }
  • 从之前请求的数据中获取数据,进行展示即可
    • 默认展示前10条数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值