This year I had an amazing opportunity to attend Apple’s Worldwide Developer Conference (WWDC) in San Jose, California.

Drag and Drop, Core ML, AR Kit are some of the APIs and frameworks which were released by Apple. Among these new API’s and frameworks, I was more fascinated by the ability to drag and drop things from one place to another within a single app or separate apps.

Overview

Drag and Drop takes advantage of the power of Multi-Touch, allowing you to move content between apps in a way that feels natural on an iPad. You can simply hold down an image, a file or selected text and drag it to wherever they want it to go. After going through Apple’s Drag and Drop Documentation, I’m going to build an app that simply allows you to drag images from a browser or another app with drag capabilities and drop it in a view.

Getting Started

To begin, open Xcode 9 (developer.apple.com/download/) and create a new Project.

Select a Single View App and Swift as your language. Change your organization name and identifier.

 

Add UIDropInteractionDelegate Protocol: 

Open ViewController.Swift and extend the UIDropInteractionDelegate Protocol.

Inside the viewDidLoad() add:

self.view.addInteraction(UIDropInteraction(delegate: self).

UIDropInteraction allows the user to drop items onto a view, employing a delegate to instantiate objects and respond to calls from the drop session.

Implement UIDropInteractionDelegate Protocol: 

Below the viewDidLoad() add the canHandle session method:

func dropInteraction(_ interaction: UIDropInteraction, canHandle
session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: UIImage.self)
}

The canHandle session method is called when the touch for a drop session is over a potential destination view, as an opportunity for your app to tell the system whether it wants to consume the drag items.

Below the canHandle session Method, add sessionDidUpdate method: 

func dropInteraction(_ interaction: UIDropInteraction,
sessionDidUpdate session: UIDropSession) -> UIDropProposal {
return UIDropProposal(operation: .copy)
}

This delegate method is called when a drop session has changed in any of the following ways: the touch point for its visual representation has entered the drop interaction’s view, or has moved within the view, or drag items were added to the session while within the view.

For a view to be eligible to accept the data from a drop session, you must finally implement the performDrop method:

func dropInteraction(_ interaction: UIDropInteraction, performDrop

session: UIDropSession) {
for item in session.items {
item.itemProvider.loadObject(ofClass: UIImage.self,
completionHandler: {(obj, err) in
if let err = err {
print(“Errror “, err)
}
guard let draggedItem = obj as? UIImage else
{return}
DispatchQueue.main.async {
let imageView = UIImageView(image: draggedItem)
self.view.addSubview(imageView)
let centerPoint = session.location(in:
self.view)
imageView.center = centerPoint
}
})
}
}

performDrop is an implementation to request and consume the data from the drop session’s item providers.

Testing

We can now open Safari or your Photos app as a Slider-over and search for an image. Now simply hold down any of the images and drag over to the DragDropDemo app.

Conclusion

You can download the full source code from my Github Repository github.com/gee-whiz/DragTwo

Reference: developer.apple.com/ios/drag-and-drop/