I am building a macOS application using SwiftUI and I’m implementing a drag and drop feature within a List. I want to visually preview the drop locations while the items are being dragged, but I’m not sure how to achieve this. Here’s the relevant part of my code:
import SwiftUI
struct ContentView: View {
@State var values = [
"a", "b", "c", "d",
"e", "f", "g", "h"
]
var body: some View {
List {
ForEach(0..<values.count, id: .self) { index in
HStack {
Image(systemName: "line.horizontal.3")
TextField("Value", text: $values[index])
}
}
.onMove { indices, newOffset in
values.move(
fromOffsets: indices,
toOffset: newOffset
)
}
}
}
}
As seen in the code, I am using onMove to handle the reordering. I would like to have a visual indication of where the item will drop when the user drags an item around. How can I implement a drop preview in this context?
Any suggestions or examples would be greatly appreciated!