Suppose I want to move an image to somewhere else within the viewController, what need to be done?
Example code:
https://github.com/Sylvia-YiyinShen/SimpleDragDropDemo
UIDragInteraction & UIDropInteraction
We have to introduce both of the above interactions. Since we want to drag the image, so the UIDragInteraction should be added to the image. Dropping somewhere in the viewController, the view of viewController which accepts the image should have an UIDropInteraction added.
UIDropInteractionDelegate & UIDragInteractionDelegate
Make the ViewController conform to these two delegates, implement the following functions
1. func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem]
Return the item should be dragged
2. func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal
Define the drop operation type
3. func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool
Define if the item can be dropped
4. func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession)
Define what should be changed when dropped
No comments:
Post a Comment