Customizing the drag and drop experience

Sometimes you will find yourself working on an app where the default implementations don't work for you. For instance, any application's drop delegate can propose a move action instead of a copy action. However, it's possible that you don't want to support this. You can restrict the allowed proposals by implementing dragInteraction(_:sessionAllowsMoveOperation:). If you only want to allow copy operations, you can return false from this method. Another restriction you can enable through a delegate method is dragInteraction(_:sessionIsRestrictedToDraggingApplication:). If you return true from this method, users won't be able to drag content from your app to another app.

Other methods on both the drag and the drop delegates are related to monitoring the state of the drag or drop session. Imagine that your app supports move proposals. When the user decides to move one or more objects from your app to another, you'll need to update the user interface accordingly once the drop is finished. You can implement dragInteraction(_:sessionDidTransferItems:) to implement this or perform any other cleanup that you might want to do after a successful drop session. For instance, you could show an alert to let the user know that data was transferred successfully.

In addition to logic, you can also provide custom previews and perform animations alongside the animations performed by the drag and drop sessions. All methods for this are defined in the UIDropInteractionDelegate and UIDragInteractionDelegate protocols as optional methods. In many cases, you won't need to implement many customizations for drag and drop. If you do find yourself wanting to customize part of the drag and drop experience, the chances are that a quick glance at the available methods on the delegate protocols will guide you in the right direction.

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

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