Skip to content

Commit

Permalink
Some minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Oct 20, 2023
1 parent 6626d2b commit 2f04723
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Sources/General/ZLGeneralDefine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func ZLMainAsync(after: TimeInterval = 0, handler: @escaping (() -> Void)) {
}

func zl_debugPrint(_ message: Any...) {
message.forEach { debugPrint($0) }
// message.forEach { debugPrint($0) }
}

func zlLoggerInDebug(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) {
Expand Down
188 changes: 102 additions & 86 deletions Sources/General/ZLPhotoPreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ZLPhotoPreviewController: UIViewController {
setupUI()

addPopInteractiveTransition()
resetSubViewStatus()
resetSubviewStatus()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -356,7 +356,11 @@ class ZLPhotoPreviewController: UIViewController {
selPhotoPreview?.selectBlock = { [weak self] model in
self?.scrollToSelPreviewCell(model)
}
selPhotoPreview?.beginSortBlock = { [weak self] in
self?.resetSubviewStatusWhenDraging(enable: false)
}
selPhotoPreview?.endSortBlock = { [weak self] models in
self?.resetSubviewStatusWhenDraging(enable: true)
self?.refreshCurrentCellIndex(models)
}
bottomView.addSubview(selPhotoPreview!)
Expand All @@ -374,6 +378,14 @@ class ZLPhotoPreviewController: UIViewController {
view.bringSubviewToFront(navView)
}

private func resetSubviewStatusWhenDraging(enable: Bool) {
collectionView.isScrollEnabled = enable
navView.isUserInteractionEnabled = enable
editBtn.isUserInteractionEnabled = enable
originalBtn.isUserInteractionEnabled = enable
doneBtn.isUserInteractionEnabled = enable
}

private func createBtn(_ title: String, _ action: Selector, _ isDone: Bool = false) -> UIButton {
let btn = UIButton(type: .custom)
btn.titleLabel?.font = ZLLayout.bottomToolTitleFont
Expand All @@ -399,7 +411,9 @@ class ZLPhotoPreviewController: UIViewController {
popInteractiveTransition?.shouldStartTransition = { [weak self] point -> Bool in
guard let `self` = self else { return false }

if !self.hideNavView, self.navView.frame.contains(point) || self.bottomView.frame.contains(point) {
if !self.hideNavView, self.navView.frame.contains(point) ||
self.bottomView.frame.contains(point) ||
self.selPhotoPreview?.isDraging == true {
return false
}

Expand Down Expand Up @@ -447,7 +461,7 @@ class ZLPhotoPreviewController: UIViewController {
}
}

private func resetSubViewStatus() {
private func resetSubviewStatus() {
guard let nav = navigationController as? ZLImageNavController else {
zlLoggerInDebug("Navigation controller is null")
return
Expand Down Expand Up @@ -545,7 +559,7 @@ class ZLPhotoPreviewController: UIViewController {

config.didDeselectAsset?(currentModel.asset)

resetSubViewStatus()
resetSubviewStatus()
} else {
if !canAddModel(currentModel, currentSelectCount: nav.arrSelectedModels.count, sender: self) {
return
Expand All @@ -562,7 +576,7 @@ class ZLPhotoPreviewController: UIViewController {

config.didSelectAsset?(currentModel.asset)

self?.resetSubViewStatus()
self?.resetSubviewStatus()
}
}
}
Expand Down Expand Up @@ -628,7 +642,7 @@ class ZLPhotoPreviewController: UIViewController {
currentModel.editImageModel = nil
nav?.arrSelectedModels.removeAll { $0 == currentModel }
selPhotoPreview?.removeSelModel(model: currentModel)
resetSubViewStatus()
resetSubviewStatus()
let index = uiConfig.sortAscending ? arrDataSources.lastIndex { $0 == currentModel } : arrDataSources.firstIndex { $0 == currentModel }
if let index = index {
collectionView.reloadItems(at: [IndexPath(row: index, section: 0)])
Expand Down Expand Up @@ -714,7 +728,7 @@ class ZLPhotoPreviewController: UIViewController {
if nav?.arrSelectedModels.contains(where: { $0 == model }) == false {
model.isSelected = true
nav?.arrSelectedModels.append(model)
self.resetSubViewStatus()
self.resetSubviewStatus()
self.selPhotoPreview?.addSelModel(model: model)
} else {
self.selPhotoPreview?.refreshCell(for: model)
Expand Down Expand Up @@ -781,8 +795,8 @@ extension ZLPhotoPreviewController {
return
}
currentIndex = page
resetSubViewStatus()
selPhotoPreview?.currentShowModelChanged(model: arrDataSources[currentIndex])
resetSubviewStatus()
selPhotoPreview?.changeCurrentModel(to: arrDataSources[currentIndex])
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
Expand Down Expand Up @@ -875,7 +889,8 @@ extension ZLPhotoPreviewController: UICollectionViewDataSource, UICollectionView

// MARK: 下方显示的已选择照片列表

class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDragDelegate, UICollectionViewDropDelegate {
// UICollectionViewDragDelegate, UICollectionViewDropDelegate
class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate {
private lazy var collectionView: UICollectionView = {
let layout = ZLCollectionViewFlowLayout()
layout.itemSize = CGSize(width: 60, height: 60)
Expand All @@ -892,15 +907,19 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
view.alwaysBounceHorizontal = true
ZLPhotoPreviewSelectedViewCell.zl.register(view)

if #available(iOS 11.0, *) {
view.dragDelegate = self
view.dropDelegate = self
view.dragInteractionEnabled = true
view.isSpringLoaded = true
} else {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction))
view.addGestureRecognizer(longPressGesture)
}
// if #available(iOS 11.0, *) {
// view.dragDelegate = self
// view.dropDelegate = self
// view.dragInteractionEnabled = true
// view.isSpringLoaded = true
// } else {
// let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction))
// view.addGestureRecognizer(longPressGesture)
// }

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction))
longPressGesture.delegate = self
view.addGestureRecognizer(longPressGesture)

return view
}()
Expand All @@ -909,10 +928,12 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti

private var currentShowModel: ZLPhotoModel

private var isDraging = false
var isDraging = false

var selectBlock: ((ZLPhotoModel) -> Void)?

var beginSortBlock: (() -> Void)?

var endSortBlock: (([ZLPhotoModel]) -> Void)?

init(selModels: [ZLPhotoModel], currentShowModel: ZLPhotoModel) {
Expand Down Expand Up @@ -941,18 +962,15 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
}
}

func currentShowModelChanged(model: ZLPhotoModel) {
func changeCurrentModel(to model: ZLPhotoModel) {
guard currentShowModel != model else {
return
}
currentShowModel = model

if let index = arrSelectedModels.firstIndex(where: { $0 == self.currentShowModel }) {
collectionView.performBatchUpdates({
self.collectionView.scrollToItem(at: IndexPath(row: index, section: 0), at: .centeredHorizontally, animated: true)
}) { _ in
self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
}
collectionView.scrollToItem(at: IndexPath(row: index, section: 0), at: .centeredHorizontally, animated: true)
collectionView.reloadData()
} else {
collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)
}
Expand Down Expand Up @@ -988,6 +1006,7 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
return
}
isDraging = true
beginSortBlock?()
collectionView.beginInteractiveMovementForItem(at: indexPath)
} else if gesture.state == .changed {
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: collectionView))
Expand All @@ -998,6 +1017,7 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
} else {
isDraging = false
collectionView.cancelInteractiveMovement()
endSortBlock?(arrSelectedModels)
}
}

Expand All @@ -1012,58 +1032,52 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
}

// MARK: iOS11 拖动

@available(iOS 11.0, *)
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
isDraging = true
let itemProvider = NSItemProvider()
let item = UIDragItem(itemProvider: itemProvider)
return [item]
}

@available(iOS 11.0, *)
func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if collectionView.hasActiveDrag {
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
return UICollectionViewDropProposal(operation: .forbidden)
}

@available(iOS 11.0, *)
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
isDraging = false
guard let destinationIndexPath = coordinator.destinationIndexPath else {
return
}
guard let item = coordinator.items.first else {
return
}
guard let sourceIndexPath = item.sourceIndexPath else {
return
}

if coordinator.proposal.operation == .move {
collectionView.performBatchUpdates({
let moveModel = self.arrSelectedModels[sourceIndexPath.row]

self.arrSelectedModels.remove(at: sourceIndexPath.row)

self.arrSelectedModels.insert(moveModel, at: destinationIndexPath.row)

collectionView.deleteItems(at: [sourceIndexPath])
collectionView.insertItems(at: [destinationIndexPath])
}, completion: nil)

coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)

endSortBlock?(arrSelectedModels)
}
}

@available(iOS 11.0, *)
func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) {
isDraging = false
}
// iOS11 拖动cell后,部分cell无法点击,先不用这种方式
// @available(iOS 11.0, *)
// func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
// isDraging = true
// let itemProvider = NSItemProvider()
// let item = UIDragItem(itemProvider: itemProvider)
// return [item]
// }
//
// @available(iOS 11.0, *)
// func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
// if collectionView.hasActiveDrag {
// return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
// }
// return UICollectionViewDropProposal(operation: .forbidden)
// }
//
// @available(iOS 11.0, *)
// func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
// isDraging = false
// guard coordinator.proposal.operation == .move,
// let destinationIndexPath = coordinator.destinationIndexPath,
// let item = coordinator.items.first,
// let sourceIndexPath = item.sourceIndexPath else {
// return
// }
//
// let moveModel = arrSelectedModels[sourceIndexPath.row]
// arrSelectedModels.remove(at: sourceIndexPath.row)
// arrSelectedModels.insert(moveModel, at: destinationIndexPath.row)
//
// collectionView.performBatchUpdates {
// collectionView.deleteItems(at: [sourceIndexPath])
// collectionView.insertItems(at: [destinationIndexPath])
// } completion: { _ in
// self.collectionView.reloadData()
// }
//
// coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
// endSortBlock?(arrSelectedModels)
// }
//
// @available(iOS 11.0, *)
// func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) {
// isDraging = false
// }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrSelectedModels.count
Expand All @@ -1079,16 +1093,13 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard !isDraging else {
return
}
guard !isDraging else { return }

let m = arrSelectedModels[indexPath.row]
currentShowModel = m
collectionView.performBatchUpdates({
self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}) { _ in
self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
}
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
collectionView.reloadData()

selectBlock?(m)
}

Expand All @@ -1100,6 +1111,11 @@ class ZLPhotoPreviewSelectedView: UIView, UICollectionViewDataSource, UICollecti
cell.layer.borderWidth = 0
}
}

override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let indexPath = collectionView.indexPathForItem(at: gestureRecognizer.location(in: collectionView))
return indexPath != nil
}
}

class ZLPhotoPreviewSelectedViewCell: UICollectionViewCell {
Expand Down
8 changes: 8 additions & 0 deletions Sources/General/ZLThumbnailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,9 @@ class ZLThumbnailViewController: UIViewController {
func endPopTransition() {
hiddenStatusBar = false
setNeedsStatusBarAppearanceUpdate()
if deviceIsiPad() {
view.setNeedsLayout()
}
}
}

Expand All @@ -1071,6 +1074,11 @@ extension ZLThumbnailViewController: UIGestureRecognizerDelegate {
return false
}

let pointInCollectionView = gestureRecognizer.location(in: collectionView)
if collectionView.indexPathForItem(at: pointInCollectionView) == nil {
return false
}

return true
}
}
Expand Down

0 comments on commit 2f04723

Please sign in to comment.