-
Notifications
You must be signed in to change notification settings - Fork 205
/
MultipleSectionViewController.swift
115 lines (103 loc) · 4.41 KB
/
MultipleSectionViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// MultipleSectionViewController.swift
// Example
//
// Created by JiongXing on 2019/12/6.
// Copyright © 2019 JiongXing. All rights reserved.
//
import UIKit
import JXPhotoBrowser
import Kingfisher
class MultipleSectionViewController: BaseCollectionViewController {
override class func name() -> String { "多Section场景" }
override class func remark() -> String { "举例如何做跨Section浏览" }
var sections = [[ResourceModel]]()
override func viewDidLoad() {
super.viewDidLoad()
sections.append(makeLocalDataSource())
sections.append(makeNetworkDataSource())
sections.append(makeLocalDataSource())
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return sections.count
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sections[section].count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.jx.dequeueReusableCell(BaseCollectionViewCell.self, for: indexPath)
let model = sections[indexPath.section][indexPath.item]
if let urlString = model.firstLevelUrl {
cell.imageView.kf.setImage(with: URL(string: urlString))
} else if let localName = model.localName {
cell.imageView.image = UIImage(named: localName)
}
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: false)
let browser = JXPhotoBrowser()
// 图片总数
browser.numberOfItems = {
return self.sections.reduce(0) { (result, modelArray) -> Int in
result + modelArray.count
}
}
// 刷新Cell数据。本闭包将在Cell完成位置布局后调用。
browser.reloadCellAtIndex = { context in
let (indexPath, model) = self.indexPathFor(browserIndex: context.index)
let collectionCell = collectionView.cellForItem(at: indexPath) as? BaseCollectionViewCell
let placeholder = collectionCell?.imageView.image
let browserCell = context.cell as? JXPhotoBrowserImageCell
if let urlString = model.secondLevelUrl {
let url = URL(string: urlString)
browserCell?.imageView.kf.setImage(with: url, placeholder: placeholder, options: [], completionHandler: { _ in
browserCell?.setNeedsLayout()
})
} else if let localName = model.localName {
browserCell?.imageView.image = UIImage(named: localName)
}
}
browser.transitionAnimator = JXPhotoBrowserZoomAnimator(previousView: { index -> UIView? in
let (path, _) = self.indexPathFor(browserIndex: index)
let cell = collectionView.cellForItem(at: path) as? BaseCollectionViewCell
return cell?.imageView
})
// 指定打开时定位到哪一页
browser.pageIndex = browserIndexFor(indexPath: indexPath)
// 展示
browser.show()
}
/// 计算所浏览图片处于哪个section哪个item
private func indexPathFor(browserIndex: Int) -> (IndexPath, ResourceModel) {
var section = 0, count = 0
for modelArray in sections {
var item = 0
for model in modelArray {
if count == browserIndex {
return (IndexPath(item: item, section: section), model)
}
count += 1
item += 1
}
section += 1
}
fatalError("所浏览的图片(index:\(browserIndex))找不到其在前向页面的位置!")
}
/// 计算IndexPath对应第几张图片
private func browserIndexFor(indexPath: IndexPath) -> Int {
var section = 0, count = 0
for modelArray in sections {
var item = 0
for _ in modelArray {
if section == indexPath.section && item == indexPath.item {
return count
}
count += 1
item += 1
}
section += 1
}
fatalError("无法计算\(indexPath)对应的图片序号!")
}
}