Overview
iOS 11 is world’s most advanced mobile OS. It provides most amazing possibilities for AR(Augmented Reality) to the application and some other cool stuffs as well. With iOS 11 iDevices has become much better and more capable than ever.
Drag and Drop
- This feature mainly focus for an iPad. It moves almost everything magically from one application to another application. It has been designed for large and multi touch displays.
- For an iPhone it is done within application itself.
- In tableview, there are two new delegate for drag and drop functionality
– UITableViewDragDelegate
– UITableViewDropDelegate
How to implement in your application?
First need to set delegates:
//Delegate. tableView.dragDelegate = self tableView.dragInteractionEnabled = true
How to drag items in UITableView?
UITableViewDragDelegate
//Method will allow to lift and drag item. func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { let image = arrImg[indexPath.row] let provider = NSItemProvider(object: image as NSItemProviderWriting) let item = UIDragItem(itemProvider: provider) item.localObject = image return [item] } // Multiple items lift and drag. func tableView(_ tableView: UITableView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] { let image = arrImg[indexPath.row] let provider = NSItemProvider(object: image as NSItemProviderWriting) let item = UIDragItem(itemProvider: provider) item.localObject = image return [item] }
First Method(itemsForBeginning session), will lift only one item and drag it but by implementing Second Method(itemsForAddingTo session) you can select and drag multiple items.
UITableViewDropDelegate
First need to set delegate:
//Delegate. tableView.dropDelegate = self //Method Will Check Drop Items can be Handle or not. func tableView(_ tableView: UITableView, canHandle session: UIDropSession) -> Bool { return session.canLoadObjects(ofClass: NSString.self) } //Method will allow drop animation and operations. func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal { return UITableViewDropProposal(dropOperation: .copy, intent: .automatic) } //Method will performDrop items to particular indexPath. func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) { let destinationIndexPath: IndexPath if let indexPath = coordinator.destinationIndexPath { destinationIndexPath = indexPath } else { let section = tableView.numberOfSections - 1 let row = tableView.numberOfRows(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.arrImg.insert(value, at: indexPath.row) indexPaths.append(indexPath) } tableView.insertRows(at: indexPaths, with: .automatic) } }
- Here First and Second Methods are optional. First Method(canHandle session), will check table view and can handle drop or not. Second Method(dropSessionDidUpdate session), is used for animation while dropping item and also check for valid drag session.
- Third Method(performDropWith coordinator), will perform drop of items at particular indexPath.
Swipes Actions
How to implement to your application?
Two New Methods in UITableViewDelegate.
– (leadingSwipeActionsConfigurationForRowAt indexPath)
– (trailingSwipeActionsConfigurationForRowAt indexPath)
//Method Swipe from left. func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let action = UIContextualAction(style: .normal, title: "Favourite") { (action, view, bool) in print("Favourite") bool(true) } action.image = imageLiteral(resourceName: "favourite") action.backgroundColor = UIColor.orange let configuration = UISwipeActionsConfiguration(actions: [action]) return configuration } //Method Swipe from Right. func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, bool) in print("Delete") bool(false) } action.image = imageLiteral(resourceName: "Delete") let configuration = UISwipeActionsConfiguration(actions: [action]) return configuration }
Conclusion
I hope my above blog has helped you to get your hands on iOS 11 Application Development.