Guest User

Untitled

a guest
Dec 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. @IBOutlet weak var colectionViewOne: UICollectionView! {
  2. didSet{
  3. colectionViewOne.dataSource = self
  4. colectionViewOne.delegate = self
  5. colectionViewOne.dragDelegate = self
  6. colectionViewOne.dropDelegate = self
  7. colectionViewOne.dragInteractionEnabled = true
  8. }
  9. }
  10.  
  11.  
  12. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  13. let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: 0)
  14. for item in coordinator.items {
  15. if let sourceIndexPath = item.sourceIndexPath {
  16. if let attributedString = item.dragItem.localObject as? NSAttributedString {
  17. collectionView.performBatchUpdates({
  18. temporaryModel.remove(at: sourceIndexPath.item)
  19. temporaryModel.insert(attributedString.string, at: destinationIndexPath.item)
  20. collectionView.deleteItems(at: [sourceIndexPath])
  21. collectionView.insertItems(at: [destinationIndexPath])
  22. })
  23. coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
  24. }
  25. }
  26.  
  27. }
  28. }
  29.  
  30. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  31. return session.canLoadObjects(ofClass: NSAttributedString.self)
  32. }
  33.  
  34. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  35. let isSelf = (session.localDragSession?.localContext as? UICollectionView) == collectionView
  36. return UICollectionViewDropProposal(operation: isSelf ? .move : .copy, intent: .insertAtDestinationIndexPath)
  37. }
  38.  
  39.  
  40. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  41. session.localContext = collectionView
  42. return dragItem(at: indexPath)
  43. }
  44.  
  45. func dragItem(at indexpath: IndexPath) -> [UIDragItem]{
  46. if let attributedString = ( colectionViewOne.cellForItem(at: indexpath) as? PracticeDragDropCellCollectionViewCell)?.cellTextLabel.attributedText{
  47. let dragItem = UIDragItem(itemProvider: NSItemProvider(object: attributedString))
  48. dragItem.localObject = attributedString
  49. return [dragItem]
  50. }
  51. else {
  52. return []
  53. }
  54. }
Add Comment
Please, Sign In to add comment