Understanding the drag and drop experience

The drag and drop experience on iOS is quite simple to use; pick up an item on the screen, drag it somewhere else, and let it go to make the dragged item appear in the new place. However, iOS didn't have this behavior until iOS 11. And even now, its full range of capabilities is only available on the iPad. Despite this limitation, the drag and drop experience is really powerful on both the iPhone and iPad.

Users can pick up an item from any app, and move it over to any other app that implements drag and drop, as they please. And dragging items is not even limited to just a single item, it's possible for users to pick up multiple items in a single drag session. The items that a user adds to their drag session don't have to be of the same type; this makes the drag and drop experience extremely flexible and fluid. Imagine selecting some text and a picture in Safari and dragging them over to a note you're making. Both the image and the text can be added to a note in just a single gesture.

Unfortunately, apps are not able to handle drag and drop out of the box; you'll need to do a little bit of work to support this feature. At the heart of the drag and drop experience are two protocols and two new interaction classes. Let's briefly go over the protocols and classes before going more in-depth and implementing drag and drop for your augmented reality art gallery.

The first requirement for a drag and drop session is a view that can start or receive a drag session. This ability is added to a view through either UIDropInteraction or UIDragInteraction. Both interactions are subclasses of the UIInteraction base class. UIInteraction manifests itself similarly to UIGestureRecognizer in the sense that you create an instance of it and attach it to a view. The following figure shows the relationship between a view and the drag and drop interactions:

When adding either UIDragInteraction or UIDropInteraction to UIView, you must also set up a delegate for the interaction you're adding. When you add UIDropInteraction, you must set UIDropInteractionDelegate on it. When you add UIDragInteraction, you must assign UIDragInteractionDelegate for it. The following figure illustrates the relationship between the interactions and their delegates:

Each protocol is related to a different set of responsibilities, as is suggested by their names. Let's have a look at UIDragInteractionDelegate first.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.242.165