Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @IBOutlet weak var colectionViewOne: UICollectionView! {
- didSet{
- colectionViewOne.dataSource = self
- colectionViewOne.delegate = self
- colectionViewOne.dragDelegate = self
- colectionViewOne.dropDelegate = self
- colectionViewOne.dragInteractionEnabled = true
- }
- }
- func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
- let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: 0)
- for item in coordinator.items {
- if let sourceIndexPath = item.sourceIndexPath {
- if let attributedString = item.dragItem.localObject as? NSAttributedString {
- collectionView.performBatchUpdates({
- temporaryModel.remove(at: sourceIndexPath.item)
- temporaryModel.insert(attributedString.string, at: destinationIndexPath.item)
- collectionView.deleteItems(at: [sourceIndexPath])
- collectionView.insertItems(at: [destinationIndexPath])
- })
- coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
- }
- }
- }
- }
- func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
- return session.canLoadObjects(ofClass: NSAttributedString.self)
- }
- func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
- let isSelf = (session.localDragSession?.localContext as? UICollectionView) == collectionView
- return UICollectionViewDropProposal(operation: isSelf ? .move : .copy, intent: .insertAtDestinationIndexPath)
- }
- func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
- session.localContext = collectionView
- return dragItem(at: indexPath)
- }
- func dragItem(at indexpath: IndexPath) -> [UIDragItem]{
- if let attributedString = ( colectionViewOne.cellForItem(at: indexpath) as? PracticeDragDropCellCollectionViewCell)?.cellTextLabel.attributedText{
- let dragItem = UIDragItem(itemProvider: NSItemProvider(object: attributedString))
- dragItem.localObject = attributedString
- return [dragItem]
- }
- else {
- return []
- }
- }
Add Comment
Please, Sign In to add comment