Drag and Drop Collection View cell in IOS 11 & swift 4

In this tutorial we are explain how to drag and drop collection view cell in two collection View at same screen in iPhone IOS 11. In IOS 11 apple introduced the Two type of protocols UICollectionViewDragDelegate and UICollectionViewDropDelegate, By the use of these protocol methods methods we can drag and drop the collection View cell from one collection View to other collection View.

NOTE:- please note that Drag-Drop supported in both iPhone and iPad, On iPad use Drag-Drop between different apps, same app, and same screen, But in iPhone use Drag-Drop only on similar screen.

First create the base project

Create the base project with two collectionView on same screen by the use of CollectionViewDataSource and CollectionViewDelegate methods. You can download the base project from here.

Now we start the Drag and drop Delegate for move the CollectionViewCell from on FirstCollectionView to SecondCollectionView.

Drag the collection view Cell (UICollectionViewDragDelegate protocol)

First we need to implement itemsForBeginning methods of drag delegate in which we need to return array of UIDragItem.

NOTE:- Must Be remember set the dragDelegate to self. And enable the dragInteraction.

 override func viewDidLoad() {
        topCollectionView?.dragDelegate = self
        topCollectionView.dragInteractionEnabled = true

itemsForBeginning -> Provide items to begin a drag associated with a given indexPath. If an empty array is returned a drag session will not begin.
For create the UIDragItem, First we need to create NSItemProvider and that item provider pass into UIDragItem(itemProvider:)

extension ViewController: UICollectionViewDragDelegate {
    func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let dragItem = self.dragItem(forPhotoAt: indexPath)
        return [dragItem]
    /// Helper method
    private func dragItem(forPhotoAt indexPath: IndexPath) -> UIDragItem {
        let imageName = self.arrayFirst[indexPath.row]
        let image = UIImage(named: imageName)
        let itemProvider = NSItemProvider(object: imageName as NSItemProviderWriting)
        let dragItem = UIDragItem(itemProvider: itemProvider)
        dragItem.localObject = imageName
        return dragItem

Drop the collection view Cell (CollectionViewDropDelegate protocol)

Now first we set the CollectionViewDropDelegate to self for that collection view in which we want to drop the collectionViewCell

    bottomCollectionView.dropDelegate = self

Implement the method for handle the drop session for drop item. for this method DropDelegate provide the method canHandle session:. If we not handle this methods then by default this return “YES”

 func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
        return session.canLoadObjects(ofClass: NSString.self)

Now implement these methods for drop the item in collectionView

 func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {

        let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: 0)
        loadAndInsertItems(at: destinationIndexPath, with: coordinator)
    func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
        return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
    private func loadAndInsertItems(at destinationIndexPath: IndexPath, with coordinator: UICollectionViewDropCoordinator) {
        let destinationIndexPath: IndexPath
        if let indexPath = coordinator.destinationIndexPath {
            destinationIndexPath = indexPath
        } else {
            let section = bottomCollectionView.numberOfSections - 1
            let row = bottomCollectionView.numberOfItems(inSection: section)
            destinationIndexPath = IndexPath(row: row, section: section)
        coordinator.session.loadObjects(ofClass: NSString.self) { items in
            guard let string = items as? [String] else { return }
            var indexPaths = [IndexPath]()
            for (index, value) in string.enumerated() {
                let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
                self.arraySecond.insert(value, at: indexPath.row)
            self.bottomCollectionView.insertItems(at: indexPaths)

Download the complete project here.


Related Post

How to use Flutter most usable Widgets | Flutter t... We need some basic widgets for creating our first flutter app. Widgets describe what their view should look like given their current configuration and...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...
New Features XCode 12 – IDE Supported Configurations Xcode 12 beta includes SDKs for iOS 14 iPadOS 14 tvOS 14 watchOS 7 macOS 11 The Xcode 12 beta release supports o...

Write A Comment