ScanCollectionView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // DragDropViewController.swift
  3. // DragAndDropInCollectionView
  4. //
  5. // Created by Payal Gupta on 06/11/17.
  6. // Copyright © 2017 Payal Gupta. All rights reserved.
  7. //
  8. import UIKit
  9. @available(iOS 11, *)
  10. class DragDropViewController: UIViewController
  11. {
  12. //MARK: Private Properties
  13. //Data Source for CollectionView-1
  14. private var items1 = [String]()
  15. //Data Source for CollectionView-2
  16. private var items2 = [String]()
  17. //MARK: Outlets
  18. @IBOutlet weak var collectionView1: UICollectionView!
  19. @IBOutlet weak var collectionView2: UICollectionView!
  20. //MARK: View Lifecycle Methods
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. //CollectionView-1 drag and drop configuration
  24. self.collectionView1.dragInteractionEnabled = true
  25. self.collectionView1.dragDelegate = self
  26. self.collectionView1.dropDelegate = self
  27. //CollectionView-2 drag and drop configuration
  28. self.collectionView2.dragInteractionEnabled = true
  29. self.collectionView2.dropDelegate = self
  30. self.collectionView2.dragDelegate = self
  31. self.collectionView2.reorderingCadence = .fast //default value - .immediate
  32. loadImage()
  33. }
  34. private func loadImage() {
  35. do {
  36. let directoryContents = try FileManager.default.contentsOfDirectory(atPath: CCUtility.getDirectoryPDFGenerator()!)
  37. for x in directoryContents {
  38. }
  39. }
  40. catch {
  41. print(error.localizedDescription)
  42. }
  43. }
  44. //MARK: Private Methods
  45. /// This method moves a cell from source indexPath to destination indexPath within the same collection view. It works for only 1 item. If multiple items selected, no reordering happens.
  46. ///
  47. /// - Parameters:
  48. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  49. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  50. /// - collectionView: collectionView in which reordering needs to be done.
  51. private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  52. {
  53. let items = coordinator.items
  54. if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath
  55. {
  56. var dIndexPath = destinationIndexPath
  57. if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
  58. {
  59. dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  60. }
  61. collectionView.performBatchUpdates({
  62. if collectionView === self.collectionView2
  63. {
  64. self.items2.remove(at: sourceIndexPath.row)
  65. self.items2.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  66. }
  67. else
  68. {
  69. self.items1.remove(at: sourceIndexPath.row)
  70. self.items1.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  71. }
  72. collectionView.deleteItems(at: [sourceIndexPath])
  73. collectionView.insertItems(at: [dIndexPath])
  74. })
  75. coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
  76. }
  77. }
  78. /// This method copies a cell from source indexPath in 1st collection view to destination indexPath in 2nd collection view. It works for multiple items.
  79. ///
  80. /// - Parameters:
  81. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  82. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  83. /// - collectionView: collectionView in which reordering needs to be done.
  84. private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  85. {
  86. collectionView.performBatchUpdates({
  87. var indexPaths = [IndexPath]()
  88. for (index, item) in coordinator.items.enumerated()
  89. {
  90. let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
  91. if collectionView === self.collectionView2
  92. {
  93. self.items2.insert(item.dragItem.localObject as! String, at: indexPath.row)
  94. }
  95. else
  96. {
  97. self.items1.insert(item.dragItem.localObject as! String, at: indexPath.row)
  98. }
  99. indexPaths.append(indexPath)
  100. }
  101. collectionView.insertItems(at: indexPaths)
  102. })
  103. }
  104. }
  105. // MARK: - UICollectionViewDataSource Methods
  106. @available(iOS 11, *)
  107. extension DragDropViewController : UICollectionViewDataSource
  108. {
  109. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
  110. {
  111. return collectionView == self.collectionView1 ? self.items1.count : self.items2.count
  112. }
  113. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
  114. {
  115. if collectionView == self.collectionView1
  116. {
  117. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! ScanCell
  118. cell.customImageView?.image = UIImage(named: self.items1[indexPath.row])
  119. cell.customLabel.text = self.items1[indexPath.row].capitalized
  120. return cell
  121. }
  122. else
  123. {
  124. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! ScanCell
  125. cell.customImageView?.image = UIImage(named: self.items2[indexPath.row])
  126. cell.customLabel.text = self.items2[indexPath.row].capitalized
  127. return cell
  128. }
  129. }
  130. }
  131. // MARK: - UICollectionViewDragDelegate Methods
  132. @available(iOS 11, *)
  133. extension DragDropViewController : UICollectionViewDragDelegate
  134. {
  135. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]
  136. {
  137. let item = collectionView == collectionView1 ? self.items1[indexPath.row] : self.items2[indexPath.row]
  138. let itemProvider = NSItemProvider(object: item as NSString)
  139. let dragItem = UIDragItem(itemProvider: itemProvider)
  140. dragItem.localObject = item
  141. return [dragItem]
  142. }
  143. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem]
  144. {
  145. let item = collectionView == collectionView1 ? self.items1[indexPath.row] : self.items2[indexPath.row]
  146. let itemProvider = NSItemProvider(object: item as NSString)
  147. let dragItem = UIDragItem(itemProvider: itemProvider)
  148. dragItem.localObject = item
  149. return [dragItem]
  150. }
  151. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters?
  152. {
  153. if collectionView == collectionView1
  154. {
  155. let previewParameters = UIDragPreviewParameters()
  156. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 25, y: 25, width: 120, height: 120))
  157. return previewParameters
  158. }
  159. return nil
  160. }
  161. }
  162. // MARK: - UICollectionViewDropDelegate Methods
  163. @available(iOS 11, *)
  164. extension DragDropViewController : UICollectionViewDropDelegate
  165. {
  166. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool
  167. {
  168. return session.canLoadObjects(ofClass: NSString.self)
  169. }
  170. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal
  171. {
  172. if collectionView === self.collectionView1
  173. {
  174. if collectionView.hasActiveDrag
  175. {
  176. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  177. }
  178. else
  179. {
  180. return UICollectionViewDropProposal(operation: .forbidden)
  181. }
  182. }
  183. else
  184. {
  185. if collectionView.hasActiveDrag
  186. {
  187. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  188. }
  189. else
  190. {
  191. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  192. }
  193. }
  194. }
  195. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)
  196. {
  197. let destinationIndexPath: IndexPath
  198. if let indexPath = coordinator.destinationIndexPath
  199. {
  200. destinationIndexPath = indexPath
  201. }
  202. else
  203. {
  204. // Get last index path of table view.
  205. let section = collectionView.numberOfSections - 1
  206. let row = collectionView.numberOfItems(inSection: section)
  207. destinationIndexPath = IndexPath(row: row, section: section)
  208. }
  209. switch coordinator.proposal.operation
  210. {
  211. case .move:
  212. self.reorderItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
  213. break
  214. case .copy:
  215. self.copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  216. default:
  217. return
  218. }
  219. }
  220. }