针对Swift项目的框架(Moya+HandyJSON+Rx)
2018-08-13 本文已影响125人
Mr大喵喵
仿照有妖气的框架实践
- 针对Swift项目的框架实例总结
效果实现

基本框架布局

第三方框架的使用

一 、关于基础类的内容

1.1上代码(关于BaseView)
Cell部分
import UIKit
import Reusable
// Mark 遵守Reusable协议
class BCBaseCollectionViewCell: UICollectionViewCell, Reusable {
override init(frame: CGRect) {
super.init(frame: frame)
configUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func configUI() {}
}
HeaderFooterView部分
import UIKit
class BCBaseTableViewHeaderFooterView: UITableViewHeaderFooterView, Reusable {
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
configUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func configUI() {}
}
以及另外的关于CollectonView的
- 使用实例
注册阶段
lazy var vipColl: UICollectionView = {[weak self] in
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 5
layout.minimumLineSpacing = 6
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
let width = floor((kScreenWidth - 10.0) / 3.0)
layout.itemSize = CGSize(width: width, height: 240)
let coll = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
coll.backgroundColor = UIColor(r: 242, g: 242, b: 242)
coll.register(cellType: BCComicCCell.self)
coll.register(supplementaryViewType: BCComicSeactionHeader.self, ofKind: UICollectionElementKindSectionHeader)
coll.register(supplementaryViewType: BCComicSeactionFoot.self, ofKind: UICollectionElementKindSectionFooter)
self?.bcTabView = coll
coll.bcFoot = BCRefreshTipKissFooter(with: "VIP用户专享\nVIP用户可以免费阅读全部漫画哦~")
coll.delegate = self
coll.dataSource = self
return coll
}()
使用阶段
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let comicList = vipLists?[indexPath.section]
let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: BCComicCCell.self)
cell.style = .withTitle
cell.model = comicList?.comics?[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, for: indexPath, viewType: BCComicSeactionHeader.self)
let comicList = vipLists?[indexPath.section]
headerView.bcModel = comicList
// headerView.delegate = self
return headerView
}else {
let footView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, for: indexPath, viewType: BCComicSeactionFoot.self)
return footView
}
}
在自定义的Cell或者Head或者Foot中
override func configUI() {} 的方法里实现布局即可
Class类的层级分布

记忆点
- SnpKit扩展使用
// 对于SnpKit的扩展 多控件组的约束
let buttonArray = [deviceDirectionButton,lightButton,chapterButton];
buttonArray.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 60, leadSpacing: 40, tailSpacing: 40)
buttonArray.snp.makeConstraints {
$0.top.equalTo(menuSlider.snp.bottom).offset(10)
$0.bottom.equalToSuperview()
}