https://github.com/targetcloud/DYZB
使用Alamofire、Kingfisher第三方框架,架构采用使用MVVM,学习swift3.0的最佳练手项目,欢迎大家github上面去download后观摩
代码预览
// HttpTools.swift
// Copyright © 2016年 targetcloud. All rights reserved.
import UIKit
import Alamofire
enum MethodType {
case get
case post
}
class HttpTools {
class func requestData(_ type : MethodType, URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping (_ result : Any) -> ()) {
let method = type == .get ? HTTPMethod.get : HTTPMethod.post
Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { (response) in
guard let result = response.result.value else {
print(response.result.error)
return
}
finishedCallback(result)
}
}
}
// BaseAnchorVC.swift
// Copyright © 2016年 targetcloud. All rights reserved.
import UIKit
private let kItemMargin : CGFloat = 10
private let kHeaderViewH : CGFloat = 50
private let NormalCellID = "NormalCellID"
private let HeaderViewID = "HeaderViewID"
let kNormalItemW = (kScreenW - 3 * kItemMargin) / 2
let kNormalItemH = kNormalItemW * 3 / 4
let kPrettyItemH = kNormalItemW * 5 / 4
let PrettyCellID = "PrettyCellID"
class BaseAnchorVC: BaseVC {
//!表示用到的时候保证有值
var baseVM : BaseVM!
lazy var collectionView : UICollectionView = {[unowned self] in
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: kNormalItemW, height: kNormalItemH)
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = kItemMargin
layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.white
collectionView.dataSource = self
collectionView.delegate = self
collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: NormalCellID)
collectionView.register(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: PrettyCellID)
collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HeaderViewID)
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
loadData()
}
}
extension BaseAnchorVC {
override func setupUI() {
contentView = collectionView
view.addSubview(collectionView)
super.setupUI()
}
}
extension BaseAnchorVC {
func loadData() {
}
}
extension BaseAnchorVC : UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return baseVM.anchorGroups.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return baseVM.anchorGroups[section].anchors.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NormalCellID, for: indexPath) as! CollectionNormalCell
cell.anchor = baseVM.anchorGroups[indexPath.section].anchors[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HeaderViewID, for: indexPath) as! CollectionHeaderView
headerView.group = baseVM.anchorGroups[indexPath.section]
return headerView
}
}
extension BaseAnchorVC : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let anchor = baseVM.anchorGroups[indexPath.section].anchors[indexPath.item]
anchor.isVertical == 0 ? pushNormalRoomVc(anchor) : presentShowRoomVc(anchor)
}
private func presentShowRoomVc(_ anchor : AnchorModel) {
let showVc = ShowRoomVC()
showVc.anchor = anchor
present(showVc, animated: true, completion: nil)
}
private func pushNormalRoomVc(_ anchor : AnchorModel) {
let normalVc = NormalRoomVC()
normalVc.anchor = anchor
navigationController?.pushViewController(normalVc, animated: true)
}
}
// GameVC.swift
// Copyright © 2016年 targetcloud. All rights reserved.
import UIKit
class GameVC: BaseAnchorVC {
fileprivate lazy var gameVM : GameVM = GameVM()
fileprivate lazy var menuView : MenuView = {
let menuView = MenuView.menuView()
menuView.frame = CGRect(x: 0, y: -kMenuViewH, width: kScreenW, height: kMenuViewH)//设置collectionView的-y,放置menuView
return menuView
}()
}
extension GameVC {
override func setupUI() {
super.setupUI()
collectionView.addSubview(menuView)
collectionView.contentInset = UIEdgeInsets(top: kMenuViewH, left: 0, bottom: 0, right: 0)//设置内边距
}
}
extension GameVC{
override func loadData() {
baseVM = self.gameVM
gameVM.requestData {
self.collectionView.reloadData()
var gameGroups = Array(self.gameVM.anchorGroups[1...15])//0...15 & gameGroups.removeFirst()
let moreGroup = AnchorGroup()
moreGroup.tag_name = "更多分类"
gameGroups.append(moreGroup)
self.menuView.groups = gameGroups
self.loadDataFinished()
}
}
}
// BaseVM.swift
// Copyright © 2016年 targetcloud. All rights reserved.
import UIKit
class BaseVM {
lazy var anchorGroups : [AnchorGroup] = [AnchorGroup]()
func loadAnchorData(isGroupData : Bool, URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping () -> ()) {
HttpTools.requestData(.get, URLString: URLString, parameters: parameters) { (result) in
guard let dict = result as? [String : Any] else { return }
guard let arr = dict["data"] as? [[String : Any]] else { return }
if isGroupData {
for dict in arr {
self.anchorGroups.append(AnchorGroup(dict: dict))
}
} else {
let group = AnchorGroup()
for dict in arr {
group.anchors.append(AnchorModel(dict: dict))
}
self.anchorGroups.append(group)
}
finishedCallback()
}
}
}
效果预览
大家在下载代码时,顺便给个STAR,谢谢,最后大家有什么问题可以mail与我:callzjy@126.com