Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. //
  2. // DragNDropViewController.swift
  3. // Delta7
  4. //
  5. // Created by Oleksandr Lysenkov on 8/31/19.
  6. // Copyright © 2019 Oleksandr Lysenkov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class DragNDropViewController: UIViewController {
  12.  
  13. @IBOutlet weak var top: UICollectionView!
  14. @IBOutlet weak var bottom: UICollectionView!
  15.  
  16. var topColors: [String] = ["#4FBCCA", "#3A9CA9", "#BE0E6D", "#A8045C"]
  17. var bottomColors: [String] = ["#4FBCCA", "#3A9CA9", "#BE0E6D", "#A8045C"]
  18.  
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21.  
  22.  
  23.  
  24. top.delegate = self
  25. top.dataSource = self
  26. top.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "top")
  27. top.dragDelegate = self
  28. top.dropDelegate = self
  29. top.dragInteractionEnabled = true
  30. top.reorderingCadence = .fast
  31.  
  32. bottom.delegate = self
  33. bottom.dataSource = self
  34. bottom.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "top")
  35. bottom.dragDelegate = self
  36. bottom.dropDelegate = self
  37. bottom.dragInteractionEnabled = true
  38. bottom.reorderingCadence = .fast
  39. }
  40.  
  41. private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
  42.  
  43. let items = coordinator.items
  44.  
  45. if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath {
  46. var dIndexPath = destinationIndexPath
  47. if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
  48. {
  49. dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  50. }
  51. collectionView.performBatchUpdates({
  52. if collectionView === self.bottom {
  53. self.bottomColors.remove(at: sourceIndexPath.row)
  54. self.bottomColors.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  55. }
  56. else {
  57. self.topColors.remove(at: sourceIndexPath.row)
  58. self.topColors.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  59. }
  60. collectionView.deleteItems(at: [sourceIndexPath])
  61. collectionView.insertItems(at: [dIndexPath])
  62. })
  63. coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
  64. }
  65. }
  66.  
  67. private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
  68.  
  69. // collectionView.performBatchUpdates({
  70. // var indexPaths = [IndexPath]()
  71. // for (index, item) in coordinator.items.enumerated()
  72. // {
  73. // let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
  74. // if collectionView === self.bottom
  75. // {
  76. // self.bottomColors.insert(item.dragItem.localObject as! String, at: indexPath.row)
  77. // }
  78. // else
  79. // {
  80. // self.topColors.insert(item.dragItem.localObject as! String, at: indexPath.row)
  81. // }
  82. // indexPaths.append(indexPath)
  83. // }
  84. // collectionView.insertItems(at: indexPaths)
  85. // })
  86. guard let item = coordinator.items.first,
  87. let word = item.dragItem.localObject as? String else { return }
  88.  
  89. //print(item.dragItem.localObject as! String)
  90. let lastIndex = collectionView.numberOfItems(inSection: 0)
  91. let destinationIndexPath = IndexPath(item: lastIndex, section: 0)
  92. // var destIndexPath = destinationIndexPath
  93. // if destIndexPath.row >= collectionView.numberOfItems(inSection: 0) {
  94. // destIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  95. // }
  96. //
  97. collectionView.performBatchUpdates({
  98. if collectionView == top {
  99. topColors.append(word)
  100. topColors.remove(at: topColors.count-1)
  101. topColors.insert(item.dragItem.localObject as! String , at: destinationIndexPath.row)
  102. let index = bottomColors.firstIndex(where: { (wordTemp) -> Bool in
  103. return wordTemp == word
  104. })
  105. if let index = index {
  106. bottomColors.remove(at: index)
  107. bottom.deleteItems(at: [IndexPath(row: index, section: 0)])
  108. }
  109. collectionView.insertItems(at: [destinationIndexPath])
  110. }
  111.  
  112. if collectionView == bottom {
  113. bottomColors.append(word)
  114. bottomColors.remove(at: bottomColors.count-1)
  115. bottomColors.insert(item.dragItem.localObject as! String , at: destinationIndexPath.row)
  116. let index = topColors.firstIndex(where: { (wordTemp) -> Bool in
  117. return wordTemp == word
  118. })
  119. if let index = index {
  120. topColors.remove(at: index)
  121. top.deleteItems(at: [IndexPath(row: index, section: 0)])
  122. }
  123. collectionView.insertItems(at: [destinationIndexPath])
  124. }
  125.  
  126. })
  127. OperationQueue.main.addOperation { [weak self] in
  128. self?.top.reloadData()
  129. self?.bottom.reloadData()
  130. }
  131.  
  132. // coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
  133.  
  134. }
  135. }
  136.  
  137. extension DragNDropViewController: UICollectionViewDelegate {
  138.  
  139. }
  140.  
  141. extension DragNDropViewController: UICollectionViewDataSource {
  142. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  143. return 4
  144. }
  145.  
  146. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  147. if collectionView == top {
  148. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "top", for: indexPath)
  149. cell.backgroundColor = UIColor(hexString: topColors[indexPath.row])
  150. return cell
  151. }
  152.  
  153. if collectionView == bottom {
  154. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "top", for: indexPath)
  155. cell.backgroundColor = UIColor(hexString: bottomColors[indexPath.row])
  156. return cell
  157. }
  158. return UICollectionViewCell()
  159. }
  160. }
  161.  
  162. extension DragNDropViewController: UICollectionViewDelegateFlowLayout {
  163.  
  164. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  165. let height = (collectionView.frame.height - 20) / 2
  166. return CGSize(width: height, height: height)
  167. }
  168. }
  169.  
  170. extension DragNDropViewController: UICollectionViewDragDelegate {
  171. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  172.  
  173. if collectionView == bottom {
  174. let color = bottomColors[indexPath.row]
  175. let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
  176. let dragItem = UIDragItem(itemProvider: itemProvider)
  177. dragItem.localObject = color
  178. return [dragItem]
  179. }
  180.  
  181. if collectionView == top {
  182. let color = topColors[indexPath.row]
  183. let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
  184. let dragItem = UIDragItem(itemProvider: itemProvider)
  185. dragItem.localObject = color
  186. return [dragItem]
  187. }
  188.  
  189. return []
  190. // let item = collectionView == top ? self.topColors[indexPath.row] : self.bottomColors[indexPath.row]
  191. // let itemProvider = NSItemProvider(object: item as NSString)
  192. // let dragItem = UIDragItem(itemProvider: itemProvider)
  193. // dragItem.localObject = item
  194. // return [dragItem]
  195. }
  196.  
  197. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
  198.  
  199. if collectionView == bottom {
  200. let color = bottomColors[indexPath.row]
  201. let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
  202. let dragItem = UIDragItem(itemProvider: itemProvider)
  203. dragItem.localObject = color
  204. return [dragItem]
  205. }
  206.  
  207. if collectionView == top {
  208. let color = topColors[indexPath.row]
  209. let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
  210. let dragItem = UIDragItem(itemProvider: itemProvider)
  211. dragItem.localObject = color
  212. return [dragItem]
  213. }
  214. //
  215. return []
  216. // let item = collectionView == top ? self.topColors[indexPath.row] : self.bottomColors[indexPath.row]
  217. // let itemProvider = NSItemProvider(object: item as NSString)
  218. // let dragItem = UIDragItem(itemProvider: itemProvider)
  219. // dragItem.localObject = item
  220. // return [dragItem]
  221. }
  222.  
  223. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
  224. return nil
  225. }
  226.  
  227. }
  228.  
  229. extension DragNDropViewController: UICollectionViewDropDelegate {
  230.  
  231. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  232. return session.canLoadObjects(ofClass: NSString.self)
  233. }
  234.  
  235. func collectionView(_ collectionView: UICollectionView,
  236. dropSessionDidUpdate session: UIDropSession,
  237. withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  238. if collectionView.hasActiveDrag {
  239. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  240. } else {
  241. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  242. }
  243. // if collectionView === self.top {
  244. // if collectionView.hasActiveDrag {
  245. // return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  246. // } else {
  247. // return UICollectionViewDropProposal(operation: .forbidden)
  248. // }
  249. // } else {
  250. // if collectionView.hasActiveDrag {
  251. // return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  252. // } else {
  253. // return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  254. // }
  255. // }
  256. }
  257.  
  258. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  259.  
  260. let destinationIndexPath: IndexPath
  261. if let indexPath = coordinator.destinationIndexPath {
  262. destinationIndexPath = indexPath
  263. } else {
  264. let section = collectionView.numberOfSections - 1
  265. let row = collectionView.numberOfItems(inSection: section)
  266. destinationIndexPath = IndexPath(row: row, section: section)
  267. }
  268.  
  269. switch coordinator.proposal.operation {
  270. case .move:
  271. reorderItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
  272. break
  273. case .copy:
  274. copyItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
  275. default:
  276. return
  277. }
  278. }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement