SwiftUI从一个列表拖动到另一列表

时间:2020-05-25 08:53:05

标签: ios iphone list swiftui

我正在尝试拖放到列表之间。

我尝试过的:

我在UIKIt和使用UIViewControllerRepresentable中找到了解决方案。但这不是我想要的。

另一个解决方案是在列表中使用.onDrag {},但该解决方案在iPad上有效,而在iPhone上无效。

如何在iPhone的两个列表之间移动项目?

1 个答案:

答案 0 :(得分:0)

检查一下:

BUT->如您所写,它不适用于list(仅在iPad上)和VStack 您可以从左向右或从右向左拖动。

import SwiftUI


struct BookmarksList2: View {
    @State private var links: [URL] = [
        URL(string: "https://www.apple.com")!
    ]

    var body: some View {
        VStack {
            ForEach(links, id: \.self) { url in
                Text(url.absoluteString)
                    .onDrag {
                        NSItemProvider(object: url as NSURL)
                }
            }
            .onInsert(of: ["public.url"], perform: drop)
            .onDrop(
                of: ["public.url"],
                delegate: BookmarksDropDelegate(bookmarks: $links)
            )
        }
        .navigationBarTitle("Bookmarks")
    }
    private func drop(at index: Int, _ items: [NSItemProvider]) {
           for item in items {
               _ = item.loadObject(ofClass: URL.self) { url, _ in
                   DispatchQueue.main.async {
                       url.map { self.links.insert($0, at: index) }
                   }
               }
           }
       }
}

struct BookmarksList3: View {
    @State private var links: [URL] = [
        URL(string: "https://www.google.com")!
    ]

    var body: some View {
        VStack {
            ForEach(links, id: \.self) { url in
                Text(url.absoluteString)
                    .onDrag {
                        NSItemProvider(object: url as NSURL)
                }
            }
            .onInsert(of: ["public.url"], perform: drop)
            .onDrop(
                of: ["public.url"],
                delegate: BookmarksDropDelegate(bookmarks: $links)
            )
        }
        .navigationBarTitle("Bookmarks")
    }

    private func drop(at index: Int, _ items: [NSItemProvider]) {
        for item in items {
            _ = item.loadObject(ofClass: URL.self) { url, _ in
                DispatchQueue.main.async {
                    url.map { self.links.insert($0, at: index) }
                }
            }
        }
    }
}

struct BookmarksDropDelegate: DropDelegate {
    @Binding var bookmarks: [URL]

    func performDrop(info: DropInfo) -> Bool {
        guard info.hasItemsConforming(to: ["public.url"]) else {
            return false
        }

        let items = info.itemProviders(for: ["public.url"])
        for item in items {
            _ = item.loadObject(ofClass: URL.self) { url, _ in
                if let url = url {
                    DispatchQueue.main.async {
                        self.bookmarks.insert(url, at: 0)
                    }
                }
            }
        }

        return true
    }
}

struct ContentView : View {

    var body: some View {

        HStack {
            BookmarksList2()
            Divider()
            BookmarksList3()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}