Commit 57b50331 by Sarkizz

修正图片选择逻辑,添加提示语

parent 3f373fd4
......@@ -15,7 +15,7 @@ public final class MRAlbumCell: UICollectionViewCell {
/// 默认的勾选按钮大小
public static var checkboxSize: CGSize = CGSize(width: 30, height: 30)
/// 默认的勾选按钮非选择状态配置
public static var deselectedCheckboxOptions: MRCheckBoxOptions = .custom(size: checkboxSize, backgroundColor: .clear)
public static var deselectedCheckboxOptions: MRCheckBoxOptions = .custom(size: checkboxSize, backgroundColor: .init(0x333333, alpha: 0.5))
/// 默认的勾选按钮选择状态配置
public static var selectedCheckboxOptions: MRCheckBoxOptions = .defaultCheck(checkboxSize)
......
......@@ -27,7 +27,7 @@ open class MRAlbumDetailViewController: UIViewController {
/// 默认勾选按钮大小
public static var checkboxSize = CGSize(width: 40, height: 40)
/// 默认勾选按钮非选择状态的显示配置
public static var unCheckOptions: MRCheckBoxOptions = .custom(size: checkboxSize, backgroundColor: .clear)
public static var unCheckOptions: MRCheckBoxOptions = .custom(size: checkboxSize, backgroundColor: .init(0x333333, alpha: 0.5))
/// 默认勾选按钮选择状态的显示配置
public static var checkedOptions: MRCheckBoxOptions = .defaultCheck(checkboxSize)
......@@ -40,6 +40,10 @@ open class MRAlbumDetailViewController: UIViewController {
public var assest: PHAsset!
/// 是否选择了图片
public var isSelected = false
public var maxSelectCount = 0
public var currentSelectedCount = 0
/// 图片缓存
public var imageCache = PHCachingImageManager()
......@@ -111,6 +115,11 @@ extension MRAlbumDetailViewController {
}
@objc private func checkboxDidPress(_ sender: UIButton) {
if isSelected == false, currentSelectedCount == maxSelectCount {
"您只能选择\(maxSelectCount)个图片文件".toast()
return
}
sender.isSelected = !sender.isSelected
isSelected = sender.isSelected
}
......
......@@ -150,27 +150,34 @@ open class MRAlbumListViewController: UIViewController {
/// - Parameter cell: cell
/// - Parameter indexPath: indexPath
open func didClickOnCellCheck(_ cell: UICollectionViewCell, at indexPath: IndexPath) {
if maxCount > 0 {
if let selected = assests?.object(at: indexPath.row) {
if selectedAssests.contains(selected) {
selectedAssests.removeAll(where: { $0 == selected })
cell.isSelected = false
return
}
if selectedAssests.count >= maxCount {
return
}
selectedAssests.append(selected)
if !collectionView.allowsMultipleSelection {
confirm()
} else {
cell.isSelected = true
if let selected = assests?.object(at: indexPath.row) {
set(asset: selected, selected: true, completion: { isSelected in
cell.isSelected = isSelected
if isSelected == true, self.maxCount == 1 {
self.confirm()
}
}
})
}
// if maxCount > 0, let selected = assests?.object(at: indexPath.row) {
// if selectedAssests.contains(selected) {
// selectedAssests.removeAll(where: { $0 == selected })
// cell.isSelected = false
// return
// }
//
// if selectedAssests.count >= maxCount {
// return
// }
//
// selectedAssests.append(selected)
// if !collectionView.allowsMultipleSelection {
// confirm()
// } else {
// cell.isSelected = true
// }
// }
}
/// 添加返回按钮。该按钮只在相册属于nav的首个vc的时候创建,可以自定义。
......@@ -220,29 +227,38 @@ open class MRAlbumListViewController: UIViewController {
let vc = MRAlbumDetailViewController()
vc.assest = assest
vc.isSelected = selectedAssests.contains(assest)
vc.maxSelectCount = maxCount
vc.currentSelectedCount = selectedAssests.count
vc.imageCache = imageCache
vc.confirm = { vc, isSelected in
var removedIndex = NSNotFound
if isSelected, !self.selectedAssests.contains(assest) {
if self.selectedAssests.count == self.maxCount {
if let assests = self.assests,
let removeItem = self.selectedAssests.first {
removedIndex = assests.index(of: removeItem)
}
if removedIndex != NSNotFound,
let cell = collectionView.cellForItem(at: IndexPath(row: removedIndex, section: 0)) {
cell.isSelected = false
}
self.selectedAssests.remove(at: 0)
self.set(asset: assest, selected: isSelected, completion: { selected in
if let cell = collectionView.cellForItem(at: indexPath) {
cell.isSelected = isSelected
}
self.selectedAssests.append(assest)
} else if !isSelected {
self.selectedAssests.removeAll(where: { $0 == assest })
}
if let cell = collectionView.cellForItem(at: indexPath) {
cell.isSelected = isSelected
}
})
// var removedIndex = NSNotFound
// if isSelected, !self.selectedAssests.contains(assest) {
// if self.selectedAssests.count == self.maxCount {
// if let assests = self.assests,
// let removeItem = self.selectedAssests.first {
// removedIndex = assests.index(of: removeItem)
// }
// if removedIndex != NSNotFound,
// let cell = collectionView.cellForItem(at: IndexPath(row: removedIndex, section: 0)) {
// cell.isSelected = false
// }
//
// self.selectedAssests.remove(at: 0)
// }
// self.selectedAssests.append(assest)
// } else if !isSelected {
// self.selectedAssests.removeAll(where: { $0 == assest })
// }
// if let cell = collectionView.cellForItem(at: indexPath) {
// cell.isSelected = isSelected
// }
}
navigationController?.pushViewController(vc, animated: true)
}
......@@ -413,6 +429,28 @@ extension MRAlbumListViewController {
break
}
}
private func set(asset: PHAsset, selected: Bool, completion: ((_ selected: Bool) -> Void)? = nil) {
guard maxCount > 0 else {
completion?(false)
return
}
if selected == false || (selectedAssests.contains(asset) && selected == true) {
selectedAssests.removeAll(where: { $0 == asset })
completion?(false)
return
}
if selectedAssests.count >= maxCount {
"您只能选择\(maxCount)个图片文件".toast()
completion?(false)
return
}
selectedAssests.append(asset)
completion?(true)
}
}
extension MRAlbumListViewController {
......
......@@ -127,7 +127,6 @@ extension SAPhotoPicker {
vc.title = config[.albumTitle] as? String
vc.maxCount = count
vc.maxImageSize = imageMaxSize
vc.modalPresentationStyle = .fullScreen
vc.didConfirm = { vc, images, count in
self.complateBlock?(vc, images, nil, count, nil)
}
......@@ -136,6 +135,7 @@ extension SAPhotoPicker {
}
customAlbumSetting?(self, vc)
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .fullScreen
if let from = viewController {
from.present(nav, animated: true)
} else {
......
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