I try to implement drag and drop between two views as shown in the code bellow, when I embed the "ForEach" view inside Group its work well, but when I try to embed it in the "List" view it doesn't work and I don't know why, any one can help to solve this problem ??
import SwiftUI
import MobileCoreServices
struct DragAndDropExample: View {
var delegate = dropDelegate()
@State var data = [SomeData(id: 1), SomeData(id: 2), SomeData(id: 3), SomeData(id: 4), SomeData(id: 5)]
@State var selectedDate: [SomeData] = []
var body: some View {
VStack (spacing: 16) {
Group { // Try to maek this "List"
ForEach(data) { dat in
Image(systemName: "\(dat.data).square.fill")
.resizable()
.background(Color.blue)
.frame(width: 50, height: 50)
.cornerRadius(15)
.onDrag{
NSItemProvider(item: .some(URL(string: dat.data)! as NSSecureCoding), typeIdentifier: String(kUTTypeURL))
}
}
}
Group { // Try to maek this "List"
if selectedDate.isEmpty {
Text("Drop Here")
}else {
ForEach(selectedDate) { dat in
Text("\(dat.data)")
}
}
}
.background(Color.blue)
.onDrop(of: [String(kUTTypeURL)], delegate: delegate)
}
}
}
struct DragAndDropExample_Previews: PreviewProvider {
static var previews: some View {
DragAndDropExample()
}
}
class SomeData: Identifiable {
let id: Int
let data: String
init(id: Int) {
self.id = id
self.data = String(id)
}
}
class dropDelegate: DropDelegate {
func performDrop(info: DropInfo) -> Bool {
// Drag and drop code here...
return true
}
}