Home > Software engineering >  onDrag preview isn’t just of the dragged view but also displays the background the view is laid on t
onDrag preview isn’t just of the dragged view but also displays the background the view is laid on t

Time:04-27

I’m using an .onDrag modifier on a view that has rounded corners:

struct RootView: View {
    
    @State var dragging: Bool = false
    
    var body: some View {
        VStack {
            Color.red.cornerRadius(32)
                .frame(width: 300, height: 300)
                .onDrag {
                    dragging = true
                    return NSItemProvider(object: NSString())
                }
        }.frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.green)
    }
}

enter image description here

The problem is when I invoke a drag - due to the automatic drop shadow effect applied by the system - it doesn’t feel as though I’m dragging the card on its own. See below.

enter image description here

Is there any way to get the card to look dragged with a transparent background, as opposed to above whatever background colour it was laid on top of?

CodePudding user response:

There is content shape type for this,

Color.red.cornerRadius(32)
    .frame(width: 300, height: 300)
    .contentShape(.dragPreview, RoundedRectangle(cornerRadius: 32))  // << here !!
    .onDrag {
  • Related