You can create a Rectangle
view in SwiftUI and use its modifier onDrag
to enable the dragging functionality. The onDrag
modifier needs to return an instance of NSItemProvider
that will be used to transfer data from drag source to drop destination.
Here's the code to make a Rectangle
draggable and return "yellow" string:
main.swift276 chars12 lines
You can use this DraggableRectangle
view inside any SwiftUI container to make it draggable.
Note that the onDrag
closure captures the self.itemProvider
property and returns it. So when a drag gesture is detected on this view, self.itemProvider
will be used to provide the data (in this case, the string "yellow") to the system's drag and drop APIs.
gistlibby LogSnag