-
Notifications
You must be signed in to change notification settings - Fork 205
/
MultipleCellViewController.swift
97 lines (82 loc) · 3.27 KB
/
MultipleCellViewController.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
//
// MultipleCellViewController.swift
// Example
//
// Created by JiongXing on 2019/11/26.
// Copyright © 2019 JiongXing. All rights reserved.
//
import UIKit
import JXPhotoBrowser
class MultipleCellViewController: BaseCollectionViewController {
override class func name() -> String { "多种类视图" }
override class func remark() -> String { "支持不同的类作为项视图,如在最后一页显示更多推荐" }
override func makeDataSource() -> [ResourceModel] {
makeLocalDataSource()
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.jx.dequeueReusableCell(BaseCollectionViewCell.self, for: indexPath)
cell.imageView.image = self.dataSource[indexPath.item].localName.flatMap { UIImage(named: $0) }
return cell
}
override func openPhotoBrowser(with collectionView: UICollectionView, indexPath: IndexPath) {
let browser = JXPhotoBrowser()
browser.numberOfItems = {
self.dataSource.count + 1
}
browser.cellClassAtIndex = { index in
if index < self.dataSource.count {
return JXPhotoBrowserImageCell.self
}
return MoreCell.self
}
browser.reloadCellAtIndex = { context in
if context.index < self.dataSource.count {
let browserCell = context.cell as? JXPhotoBrowserImageCell
let indexPath = IndexPath(item: context.index, section: indexPath.section)
browserCell?.imageView.image = self.dataSource[indexPath.item].localName.flatMap { UIImage(named: $0) }
}
}
browser.transitionAnimator = JXPhotoBrowserZoomAnimator(previousView: { index -> UIView? in
if index < self.dataSource.count {
let path = IndexPath(item: index, section: indexPath.section)
let cell = collectionView.cellForItem(at: path) as? BaseCollectionViewCell
return cell?.imageView
}
return nil
})
browser.pageIndex = indexPath.item
browser.show()
}
}
class MoreCell: UIView, JXPhotoBrowserCell {
weak var photoBrowser: JXPhotoBrowser?
static func generate(with browser: JXPhotoBrowser) -> Self {
let instance = Self.init(frame: .zero)
instance.photoBrowser = browser
return instance
}
var onClick: (() -> Void)?
lazy var button: UIButton = {
let btn = UIButton(type: .custom)
btn.setTitle(" 更多 + ", for: .normal)
btn.setTitleColor(UIColor.darkText, for: .normal)
return btn
}()
required override init(frame: CGRect) {
super.init(frame: .zero)
backgroundColor = .white
addSubview(button)
button.addTarget(self, action: #selector(click), for: .touchUpInside)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
button.sizeToFit()
button.center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
}
@objc private func click() {
photoBrowser?.dismiss()
}
}