Home / IOS Development / ios – SwiftUI | onDrag – customize (dragItem) preview of the appearance of the image

ios – SwiftUI | onDrag – customize (dragItem) preview of the appearance of the image

I have been wondering if there is any way to customize preview image of view which is dragged when using onDrag?

As you may see, is preview image is by default a slightly larger opacity image of the display.

From what I have found, a preview image generated at the very beginning of the drag process. But I could not find a way to change it.

What I mean by customizing is having someone customized image or a preview image of a custom view. (Both without standard opacity)

Does anyone have an idea?

I have tried to use previewImageHandler and generally read a lot about NSItemProvider. But to me it seems like this is something that is not possible for SwiftUI yet?

With UIKit one could only have adapted UIDragItem ̵

1; something like that that uses previewProvider: Her

Here is my demo code:

struct ContentView: View {
    var body: some View {
            .onDrag({ NSItemProvider() })
    private struct DraggedView: View {
        var body: some View {
            RoundedRectangle(cornerRadius: 20)
                .frame(width: 120, height: 160)

I want to use this to drag and drop within one LazyVGrid, so customized gestures is unfortunately not an option.

Another idea I had would be to have a gesture at the same time that first changes item to be drawn to something else and then onDrag starts and returns NSItemProvider with preview image which would be the one I want. But I could not let these two movements go at the same time, you had to reject one first to start the other.

Thank you!

Source link