TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

determine what is being dragged

Forums > SwiftUI

I understand how to use the draggable and dropDestination view modifiers to implement drag and drop. Now I want to know what is being dragged during the drag so I can decide on a border color to apply to the drop area. How can I determine what is being dragged?


You could create an enum containing all your dragable elements. Then add an optional variable to your view and set it when you start your drag. With the onChange modifier you can start an action when this variable is changed.


@hatsushira How can I detect the start of the drag and know which "draggable" is being dragged? That's the piece I'm missing.


Does the onDrag modifier not work for this?


@MarkVolkmann joins HWS, then jumps into the deep end with:

How can I detect the start of the drag and know which "draggable" is being dragged?

Mark! Welcome to HackingWithSwift. I've worked with great programmers teachers here willing to help answer basic (and advanced!) SwiftUI questions. But sometimes, to guide you to the right destination, it's good to know where you are.

@twoStraws covers drag and drop in some of his lessons. Have you followed his lessons on this? Have you been successful in getting his code to work?

See --> How to Drag and Drop in SwiftUI

Keep coding!

Please come back and let us know.


@Hatsushira It seems that onDrag doesn't work when you also use draggable which is what I am doing. You can see my short code for this at


@Obelix The "How to Drag and Drop in SwiftUI" post you shared is exactly the approach I am using. But as far as I can see it does not address detecting when a drag begins and determining what is being dragged before it is dropped. That's the last piece of the puzzle for what I am trying to do which is to add a colored border to compatiable drop destinations during the drag. You can see my short code at


Obelix is still unsure if @MarkVolkman is following the 100 Days of SwiftUI. Perhaps we'll never know. But we'll try to help, just the same.

@MarkVolkman? Does this move you closer to you education goals?

struct DragDropExample: View {
    private let examples              = ["Paul Hudson", "Taylor Swift", "Obelix"]
    @State private var dragText       = "Life, Universe, and Everything"
    @State private var dropTargetText = ""

    // Drag text. Display these views whilst dragging.
    let twoStraws = Text("twoStraws").font(.largeTitle).foregroundColor(.red)
    let singer    = Text("Bejeweled").foregroundColor(.indigo)
    let teacher   = Text("Teacher"  ).font(.title3).foregroundColor(.green)

    var body: some View {
        VStack {
                    NSItemProvider(object: self.dragText as NSString)
                }) {
                    // Here!  dragText is the object user is dragging!
                    // Adjust the drag item's look whilst dragging.
                    switch dragText {
                    case "Paul Hudson":   twoStraws
                    case "Taylor Swift":  singer
                    case "Obelix":        teacher
                    default: teacher

            // This is the drop target.
            RoundedRectangle(cornerRadius: 20)
                .fill(.teal.gradient) // LOVE the gradient!
                .frame(width: 200, height: 100)
                // Nota bene! You may drag more than one object!
                .onDrop(of: [dragText], delegate: ThingToDrop(text: $dragText, dropTargetText: $dropTargetText))

            Button("Reset") {
                dragText       = examples.randomElement()!
                dropTargetText = ""
        .onAppear { dragText = examples.randomElement()! } // Grab some text.

struct ThingToDrop: DropDelegate {
    @Binding var text: String
    @Binding var dropTargetText: String

    func performDrop(info: DropInfo) -> Bool {
        dropTargetText = text
        return true // Important! Tell the drag object, you can accept it!


@Obelix I am not currently working through "100 Days of SwiftUI". Perhaps I should do that at some point, but I've been successfully using SwiftUI for over a year now and loving it! Your solution above uses onDrag and onDrop. Perhaps that is what I need to do to achieve my goal, but I'm trying to do it with the newer view modifiers draggable and dropDestination. I wonder if it's the case that when using draggable there is no way to be notified when dragging begins and what is being dragged.


Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.