-
Notifications
You must be signed in to change notification settings - Fork 205
/
DataSourceAppendViewController.swift
67 lines (59 loc) · 2.57 KB
/
DataSourceAppendViewController.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
//
// DataSourceAppendViewController.swift
// Example
//
// Created by JiongXing on 2019/11/29.
// Copyright © 2019 JiongXing. All rights reserved.
//
import UIKit
import JXPhotoBrowser
class DataSourceAppendViewController: BaseCollectionViewController {
override class func name() -> String { "无限新增图片" }
override class func remark() -> String { "浏览过程中不断新增图片,变更数据源,刷新UI" }
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
}
browser.reloadCellAtIndex = { context in
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
let path = IndexPath(item: index, section: indexPath.section)
let cell = collectionView.cellForItem(at: path) as? BaseCollectionViewCell
return cell?.imageView
})
// 监听页码变化
browser.didChangedPageIndex = { index in
// 已到最后一张
if index == self.dataSource.count - 1 {
self.appendMoreData(browser: browser)
}
}
browser.scrollDirection = .vertical
browser.pageIndex = indexPath.item
browser.show()
}
private func appendMoreData(browser: JXPhotoBrowser) {
var randomIndexes = (0..<6).map { $0 }
randomIndexes.shuffle()
randomIndexes.forEach { index in
let model = ResourceModel()
model.localName = "local_\(index)"
dataSource.append(model)
}
collectionView.reloadData()
// TODO: UIScrollView的pageEnable特性原因,不能很好衔接,效果上有点问题,还未解决
browser.reloadData()
}
}