Adopt drag interaction APIs to provide items for dragging.
- iOS 11.0+
By implementing a drag interaction delegate (
UIDrag) for a view, you enable that view to function as a drag source in your app.
Enable a View as a Drag Source
Any instance or subclass of
UIView can act as a drag source. Your first steps to make this happen are:
Create a drag interaction (a
Specify the drag interaction’s delegate (an object that conforms to the
Add the interaction to the view’s
Here’s how to do this using a custom helper method, which you would typically call within a view controller’s
Create a Drag Item
A drag item encapsulates a source app’s promises for providing a variety of data representations for one model object.
To create a drag item, implement the
drag method in your drag interaction delegate, as shown here in a minimal form:
This implementation uses the
init convenience initializer. When you instantiate a drag item, pass an object in your app’s native representation, or in the highest-fidelity representation you support. In general, ensure that the first element in the item provider’s
registered array represents the highest-fidelity data your drag interaction delegate can deliver.
To add more data representations to a drag item, as you typically would in your app, add them in fidelity order, from highest to lowest. When adding representations, you have choices:
The best option for adding multiple data representations to a drag item, in many cases, is to adopt the
NSItemprotocol in your model class. Using this protocol, you place the code for providing multiple data representations within the model class.
Understand a Drag Source in Context
drag protocol method, your source app responds to a request from the system. This request is itself triggered by the user starting to drag an item in your app’s UI. The conversation between your app and the system proceeds as shown here:
The figure above depicts the steps for constructing a drag item, in context:
The user initiates a drag activity with a long press on a view in your app, followed by moving their finger while still touching the screen. The system instantiates a drag session (an object that conforms to the
UIDragprotocol, not shown in the figure) for managing the drag activity.
The system calls the drag interaction delegate’s
dragprotocol method. Your delegate returns one or more drag items.
Interaction: items For Beginning Session:
Finally, the system populates the drag session with your drag items, ready for the user to move the drag session to a destination.