Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  2. {
  3. let items = coordinator.items
  4. if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath
  5. {
  6. var dIndexPath = destinationIndexPath
  7. if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
  8. {
  9. dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  10. }
  11. collectionView.performBatchUpdates({
  12. if collectionView === self.collectionView2
  13. {
  14. self.items2.remove(at: sourceIndexPath.row)
  15. self.items2.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  16. }
  17. else
  18. {
  19. self.items1.remove(at: sourceIndexPath.row)
  20. self.items1.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  21. }
  22. collectionView.deleteItems(at: [sourceIndexPath])
  23. collectionView.insertItems(at: [dIndexPath])
  24. })
  25. coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
  26. }
  27. }
  28.  
  29. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)
  30. {
  31. let destinationIndexPath: IndexPath
  32. if let indexPath = coordinator.destinationIndexPath
  33. {
  34. destinationIndexPath = indexPath
  35. }
  36. else
  37. {
  38. // Get last index path of table view.
  39. let section = collectionView.numberOfSections - 1
  40. let row = collectionView.numberOfItems(inSection: section)
  41. destinationIndexPath = IndexPath(row: row, section: section)
  42. }
  43.  
  44. switch coordinator.proposal.operation
  45. {
  46. case .move:
  47. self.reorderItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
  48. break
  49.  
  50. case .copy:
  51. self.copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  52.  
  53. default:
  54. return
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement