Commit 73a54df2 by Sarkizz

修正自定义多选相册的逻辑

parent c013ba33
...@@ -71,12 +71,15 @@ open class MRAlbumDetailViewController: UIViewController { ...@@ -71,12 +71,15 @@ open class MRAlbumDetailViewController: UIViewController {
open override func viewWillAppear(_ animated: Bool) { open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false) // navigationController?.setNavigationBarHidden(true, animated: false)
} }
open override func viewWillDisappear(_ animated: Bool) { open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated) super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: false) // navigationController?.setNavigationBarHidden(false, animated: false)
if state == .loading {
state = .none
}
} }
open override var prefersStatusBarHidden: Bool { open override var prefersStatusBarHidden: Bool {
...@@ -175,8 +178,8 @@ extension MRAlbumDetailViewController { ...@@ -175,8 +178,8 @@ extension MRAlbumDetailViewController {
setupMainViews() setupMainViews()
setupLayouts() setupLayouts()
resizeImageView() resizeImageView()
default: case .none:
break self.imageCache.stopCachingImagesForAllAssets()
} }
} }
......
...@@ -150,23 +150,25 @@ open class MRAlbumListViewController: UIViewController { ...@@ -150,23 +150,25 @@ open class MRAlbumListViewController: UIViewController {
/// - Parameter cell: cell /// - Parameter cell: cell
/// - Parameter indexPath: indexPath /// - Parameter indexPath: indexPath
open func didClickOnCellCheck(_ cell: UICollectionViewCell, at indexPath: IndexPath) { open func didClickOnCellCheck(_ cell: UICollectionViewCell, at indexPath: IndexPath) {
if let selected = assests?.object(at: indexPath.row) { if maxCount > 0 {
if let selected = assests?.object(at: indexPath.row) {
if selectedAssests.contains(selected) {
selectedAssests.removeAll(where: { $0 == selected }) if selectedAssests.contains(selected) {
cell.isSelected = false selectedAssests.removeAll(where: { $0 == selected })
return cell.isSelected = false
} return
}
if selectedAssests.count >= maxCount {
return if selectedAssests.count >= maxCount {
} return
}
selectedAssests.append(selected)
if !collectionView.allowsMultipleSelection { selectedAssests.append(selected)
confirm() if !collectionView.allowsMultipleSelection {
} else { confirm()
cell.isSelected = true } else {
cell.isSelected = true
}
} }
} }
} }
...@@ -183,15 +185,13 @@ open class MRAlbumListViewController: UIViewController { ...@@ -183,15 +185,13 @@ open class MRAlbumListViewController: UIViewController {
let backItem = UIBarButtonItem(customView: button) let backItem = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = backItem navigationItem.leftBarButtonItem = backItem
if collectionView.allowsMultipleSelection { let confirmBtn = UIButton(type: .custom)
let confirmBtn = UIButton(type: .custom) confirmBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
confirmBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 40) confirmBtn.setTitle("确定", for: .normal)
confirmBtn.setTitle("确定", for: .normal) confirmBtn.setTitleColor(.init(0x333333), for: .normal)
confirmBtn.setTitleColor(.init(0x333333), for: .normal) confirmBtn.addTarget(self, action: #selector(confirm), for: .touchUpInside)
confirmBtn.addTarget(self, action: #selector(confirm), for: .touchUpInside) let item = UIBarButtonItem(customView: confirmBtn)
let item = UIBarButtonItem(customView: confirmBtn) navigationItem.rightBarButtonItem = item
navigationItem.rightBarButtonItem = item
}
} }
} }
...@@ -205,7 +205,7 @@ open class MRAlbumListViewController: UIViewController { ...@@ -205,7 +205,7 @@ open class MRAlbumListViewController: UIViewController {
collectionView.allowsMultipleSelection = maxCount > 1 collectionView.allowsMultipleSelection = maxCount > 1
let count = selectedAssests.count - maxCount let count = selectedAssests.count - maxCount
if count > 0 { if count > 0 {
selectedAssests.removeLast(count) selectedAssests.removeFirst(count)
} }
} }
collectionView.reloadData() collectionView.reloadData()
...@@ -222,8 +222,22 @@ open class MRAlbumListViewController: UIViewController { ...@@ -222,8 +222,22 @@ open class MRAlbumListViewController: UIViewController {
vc.isSelected = selectedAssests.contains(assest) vc.isSelected = selectedAssests.contains(assest)
vc.imageCache = imageCache vc.imageCache = imageCache
vc.confirm = { vc, isSelected in vc.confirm = { vc, isSelected in
var removedIndex = NSNotFound
if isSelected, !self.selectedAssests.contains(assest) { if isSelected, !self.selectedAssests.contains(assest) {
self.selectedAssests.append(assest) if self.selectedAssests.count == self.maxCount {
let removedAssest = self.selectedAssests[0]
if let assests = self.assests {
removedIndex = assests.index(of: assest)
}
if removedIndex != NSNotFound,
let cell = collectionView.cellForItem(at: IndexPath(row: removedIndex, section: 0)) {
cell.isSelected = false
}
self.selectedAssests[0] = assest
} else {
self.selectedAssests.append(assest)
}
} else if !isSelected { } else if !isSelected {
self.selectedAssests.removeAll(where: { $0 == assest }) self.selectedAssests.removeAll(where: { $0 == assest })
} }
...@@ -242,10 +256,14 @@ open class MRAlbumListViewController: UIViewController { ...@@ -242,10 +256,14 @@ open class MRAlbumListViewController: UIViewController {
/// 点击确定 /// 点击确定
@objc open func confirm() { @objc open func confirm() {
ToastView.loading() if selectedAssests.count > 0 {
requestImages(with: selectedAssests, size: maxImageSize) { (images, count) in ToastView.loading()
ToastView.hide(animate: false) requestImages(with: selectedAssests, size: maxImageSize) { (images, count) in
self.didConfirm?(self, images, count) ToastView.hide(animate: false)
self.didConfirm?(self, images, count)
}
} else {
self.didCancel?(self)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment