-
Notifications
You must be signed in to change notification settings - Fork 205
/
DataSourceDeleteViewController.swift
68 lines (60 loc) · 2.91 KB
/
DataSourceDeleteViewController.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
//
// DataSourceDeleteViewController.swift
// Example
//
// Created by JiongXing on 2019/11/28.
// Copyright © 2019 JiongXing. All rights reserved.
//
import UIKit
import JXPhotoBrowser
class DataSourceDeleteViewController: 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
guard let browserCell = context.cell as? JXPhotoBrowserImageCell else {
return
}
let indexPath = IndexPath(item: context.index, section: indexPath.section)
// 必须从数据源取数据,不能取collectionCell的imageView里的图片,
// 在更改数据源后即使reloadData,也可能取不到,因为UIImageView的图片需要一个更新周期。
browserCell.imageView.image = self.dataSource[indexPath.item].localName.flatMap { UIImage(named: $0) }
browserCell.index = context.index
// 添加长按事件
browserCell.longPressedAction = { cell, _ in
self.longPress(cell: cell)
}
}
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.pageIndex = indexPath.item
browser.show()
}
private func longPress(cell: JXPhotoBrowserImageCell) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "删除", style: .destructive, handler: { _ in
self.dataSource.remove(at: cell.index)
self.collectionView.reloadData()
cell.photoBrowser?.reloadData()
// 如果删没了,关闭
cell.photoBrowser?.dismiss()
}))
alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
cell.photoBrowser?.present(alert, animated: true, completion: nil)
}
}