Skip to content

Instantly share code, notes, and snippets.

@paul1893
Last active January 4, 2018 13:13
Show Gist options
  • Save paul1893/9237f1ff79f1836a06ab028cd972a74a to your computer and use it in GitHub Desktop.
Save paul1893/9237f1ff79f1836a06ab028cd972a74a to your computer and use it in GitHub Desktop.
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: 0)
coordinator.session.progressIndicatorStyle = .none
for item in coordinator.items {
let itemProvider = item.dragItem.itemProvider
guard itemProvider.canLoadObject(ofClass: UIImage.self) else { continue }
var placeholderContext: UICollectionViewDropPlaceholderContext? = nil
let progress = itemProvider.loadObject(ofClass: UIImage.self, completionHandler: { (object, error) in
DispatchQueue.main.async {
if let image = object as? UIImage {
placeholderContext?.commitInsertion(dataSourceUpdates: { (placeholderIndexPath) in
self.imagesArray.insert(image, at: placeholderIndexPath)
})
} else {
placeholderContext?.deletePlaceholder()
}
}
})
let dropPlaceholder = UICollectionViewDropPlaceholder(insertionIndexPath: destinationIndexPath, reuseIdentifier: "PlaceholderCell")
dropPlaceholder.previewParametersProvider = { (cell) in
// Configure your placeholder cell here ...
// e.g: guard let placeholderCell = cell as? YourCustomPlaceholderCollectionViewCell else { return nil }
return UIDragPreviewParameters()
}
placeholderContext = coordinator.drop(item.dragItem, to: dropPlaceholder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment