
Swift 5.1 高帧率朋友圈实现
本篇文章介绍如何基于 IGListKit 框架实现类似朋友圈的高帧率列表,确保在大量图文混排的情况下依然保持流畅滑动。
本文基于IGListKit 4.0实现列表的高帧率滑动效果,项目地址见GitHub
话不多说,上图

创建基类控制器
所有IGListKit的视图控制器都应该继承此类,减少复用
1class BaseListVC: UIViewController {
2
3 var objects: [ListDiffable] = [ListDiffable]()
4
5 lazy var collectionView: UICollectionView = {
6 let flow = UICollectionViewFlowLayout()
7 let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
8 if #available(iOS 11.0, *) {
9 collectionView.contentInsetAdjustmentBehavior = .never
10 } else {
11 automaticallyAdjustsScrollViewInsets = false
12 }
13 collectionView.backgroundColor = UIColor.groupTableViewBackground
14 return collectionView
15 }()
16 lazy var adapter: ListAdapter = {
17 let adapter = ListAdapter(updater: ListAdapterUpdater(), viewController: self)
18 return adapter
19 }()
20
21
22 override func viewDidLoad() {
23 super.viewDidLoad()
24 view.addSubview(collectionView)
25 adapter.collectionView = collectionView
26 adapter.dataSource = self
27 }
28
29 override func viewDidLayoutSubviews() {
30 super.viewDidLayoutSubviews()
31 collectionView.frame = view.bounds
32 }
33}
34
35extension BaseListVC : ListAdapterDataSource {
36 func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
37 return objects
38 }
39
40 func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
41 return ListSectionController()
42 }
43
44 func emptyView(for listAdapter: ListAdapter) -> UIView? {
45 // 无数据时collectionView的展示
46 return nil
47 }
48}
创建MomentInfo模型
1class MomentInfo {
2 var id: Int = 0
3 ...
4}
5extension MomentInfo: ListDiffable {
6 func diffIdentifier() -> NSObjectProtocol {
7 // 区分是否为同一对象,可多属性叠加
8 return id as NSObjectProtocol
9 }
10
11 // 判断对象是否相同,不同(false)则刷新
12 func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
13 guard self === object else { return true }
14 guard let object = object as? MomentInfo else { return false }
15 return id == object.id
16 }
17}
18
19extension MomentInfo: Equatable {
20 static func == (lhs: MomentInfo, rhs: MomentInfo) -> Bool {
21 return lhs.isEqual(toDiffableObject: rhs)
22 }
23}
子类复写
- 更新数据
1//info -> MomentInfo的对象
2self.objects.append(info)
3// 数据添加完成后更新
4self.adapter.performUpdates(animated: true, completion: nil)
- 复写绑定Section
1override func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
2 // 此object的类型就是更新的self.objects的item类型
3 switch object {
4 case is MomentInfo:
5 // MomentBindingSection 继承自 ListBindingSectionController
6 let section = MomentBindingSection()
7 return section
8 default:
9 fatalError()
10 }
11}
使用ListBindingSectionController绑定多个cell
- 创建
UICollectionViewCell⚠️只能有一种样式布局,不同样式需要不同的cell
1class MomentTopCell: UICollectionViewCell {
2 ...
3}
4extension MomentTopCell: ListBindable {
5 func bindViewModel(_ viewModel: Any) {
6 guard let viewModel = viewModel as? MomentInfo else { return }
7 // 更新cell数据
8 }
9}
- 获取cell对象
1func momentTopCell(at index: Int) -> MomentTopCell {
2// guard let cell = collectionContext?.dequeueReusableCell(withNibName: MomentTopCell.jx_className, bundle: nil, for: self, at: index) as? MomentTopCell else { fatalError() }
3 guard let cell = collectionContext?.dequeueReusableCell(of: MomentTopCell.self, for: self, at: index) as? MomentTopCell else { fatalError() }
4 // 传入cell数据
5 cell.bindViewModel(object!)
6 return cell
7}
- 绑定视图模型(ViewModel),可使用枚举替代
1enum ViewModelEnum: String {
2 case top, header, image_single, location, bottom
3}
4/// 绑定viewmodels,用以区分不同cell
5func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, viewModelsFor object: Any) -> [ListDiffable] {
6 guard let object = object as? MomentInfo else { return [] }
7 var results: [ListDiffable] = []
8 if object.userInfo != nil {
9 results.append(ViewModelEnum.top.rawValue as ListDiffable)
10 }
11 if object.images.count == 1 {
12 results.append(ViewModelEnum.image_single.rawValue as ListDiffable)
13 }else {
14 results.append(ViewModelEnum.header.rawValue as ListDiffable)
15 }
16 if !object.location.isEmpty {
17 results.append(ViewModelEnum.location.rawValue as ListDiffable)
18 }
19 results.append(ViewModelEnum.bottom.rawValue as ListDiffable)
20 return results
21}
22
23/// 不同viewmodel对应的cell
24func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, cellForViewModel viewModel: Any, at index: Int) -> UICollectionViewCell & ListBindable {
25 let viewModel = ViewModelEnum(rawValue: viewModel as! String)!
26 switch viewModel {
27 case .top:
28 return momentTopCell(at: index)
29 case .image_single:
30 return momentHeaderImageCell(at: index)
31 case .header:
32 return momentHeaderCell(at: index)
33 case .location:
34 return momentLocationCell(at: index)
35 case .bottom:
36 return momentBottomCell(at: index)
37 }
38}
39/// 不同cell对应的size
40func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize {
41 guard let object = object as? MomentInfo else { fatalError() }
42 let viewModel = ViewModelEnum(rawValue: viewModel as! String)!
43 let width: CGFloat = collectionContext!.containerSize(for: self).width
44 switch viewModel {
45 case .top:
46 return CGSize(width: width, height: 400)
47 case .header, .image_single:
48 return CGSize(width: width, height: object.cellHeight)
49 case .location:
50 return CGSize(width: width, height: 30)
51 case .bottom:
52 return CGSize(width: width, height: 30)
53 }
54}
- 标题: Swift 5.1 高帧率朋友圈实现
- 作者: Jerome Xiong
- 创建于: 2020-04-29 19:48:59
- 链接: https://jeromexiong.github.io/2020/04/29/iOS-Swift/Swift-5-1-高帧率朋友圈实现/
- 版权声明:本文章采用 CC BY-NC-SA 4.0 进行许可。
评论