[英]SwiftUI DropDestination Using Custom Transferable Type and Using UTI / UTType Not Working
I am trying to implement drag and drop using the Transferable protocol within my app.我正在尝试在我的应用程序中使用 Transferable 协议实现拖放。 I followed the below tutorial but can't get it to work.
我按照以下教程进行操作,但无法正常工作。 I created this very simple example and it still doesn't work.
我创建了这个非常简单的示例,但它仍然不起作用。 I must be missing something simple now.
我现在一定缺少一些简单的东西。 https://serialcoder.dev/text-tutorials/swiftui/first-experience-with-transferable-implementing-drag-and-drop-in-swiftui/
https://serialcoder.dev/text-tutorials/swiftui/first-experience-with-transferable-implementing-drag-and-drop-in-swiftui/
import SwiftUI
import Foundation
import UniformTypeIdentifiers
struct ContentView: View {
@State private var draggedOutput: String = ""
var body: some View {
VStack(spacing: 50) {
Text("Text")
.font(.title)
.draggable("DRAGGED TEXT")
Text("Custom Transferable")
.font(.title)
.draggable(CustomTransferable())
Divider()
Text(draggedOutput)
.font(.largeTitle)
.frame(width: 250, height: 250)
.background(Color.green)
.dropDestination(for: String.self) { items, location in
draggedOutput = items.first!
return true
}
.dropDestination(for: CustomTransferable.self) { items, location in
draggedOutput = items.first!.id.uuidString
return true
}
}
.padding()
}
}
struct CustomTransferable: Identifiable, Codable, Transferable {
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(for: CustomTransferable.self, contentType: UTType(exportedAs: "com.gmail.Me.D.ExtraInfoAudio"))
}
let id: UUID
init(id: UUID = UUID()) {
self.id = id
}
}
I switched the order of the dropDestination modifiers and my custom drop started to work and the standard String one stopped working.我切换了 dropDestination 修饰符的顺序,我的自定义 drop 开始工作,标准 String one 停止工作。 I don't see it in the documentation or the WWDC talk for Transferable but it seems that only 1 dropDestination works
我没有在 Transferable 的文档或 WWDC 谈话中看到它,但似乎只有 1 个 dropDestination 有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.