iOS 11 — Multitasking — Drag and Drop — part 1


After the long wait, Apple added support of Drag and Drop in iOS 11. Drag and Drop is the master stroke in iOS multitasking. Now user can move content inside app or between apps, user can use one hand to hold or drag content and other hand to launch app from Application Dock system and drop content to other app.

In iPhone - Drag and Drop only supported within your application. 😞😢😫

UITableView and UICollectionView both now support drag and drop. Apple introduces some new delegate method add support.


In case of UITableView to UITableView,

→ Conform UITableViewDragDelegate or UITableViewDropDelegate protocols for your class.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITableViewDragDelegate, UITableViewDropDelegate{
}

→ Bind delegate for drag event.

objTableView1.dragDelegate = self

→ Bind delegate for drop event.

objTableView1.dropDelegate = self

→ Enable drag interaction for UITableView.

objTableView1.dragInteractionEnabled = true

→ Implementing Protocol required methods.

func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
code
}

func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
code
}

The itemsForBeginning method is called when the user has initiated a drag on a cell, and return an array of drag item.

The performDropWith method provides destination UITableView object as class UITableViewDropCoordinator with thedestinationIndexPath property, which returns where the user wants to drop cell content.

ThedestinationIndexPath property is optional, so if it is nil then consider that user wanted to drop content end of the UITableView.

Implementation performDropWith method will contain insertRows() to add new drop content in destination UITableView.

Whoo…!! That’s all. We are done. 👍



If you find this post helpful, please recommend it for others to read.

Follow me for the next interesting article. 😇
I hope you liked, have a good dev day 😀
Please leave comments below if you have new ideas or suggestions.

Thanks for the reading 👍

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.