【发布时间】:2020-04-15 09:13:03
【问题描述】:
我正在尝试使用自定义类型标识符在 macOS 上实现拖放以避免冲突,但它似乎不起作用。首先,这是一个带有 public 和已知标识符的工作示例:
struct ReleaseView: View {
let id: Int
var body: some View {
GeometryReader { _ in
VStack(spacing: 16) {
Image(nsImage: NSImage(named: NSImage.networkName)!)
.contentShape(Rectangle())
.onDrag {
return NSItemProvider(item: "\(self.id)" as NSString, typeIdentifier: NSPasteboard.PasteboardType.string.rawValue)
}
DropZone()
}
}
}
}
struct DropZone: View {
@State var isDragging = false
var body: some View {
RoundedRectangle(cornerRadius: 16)
.stroke(style: StrokeStyle(lineWidth: 4, dash: [8, 8]))
.background(isDragging ? Color.secondary : Color.clear)
.frame(width: 100, height: 100)
.onDrop(of: [NSPasteboard.PasteboardType.string.rawValue], isTargeted: self.$isDragging) { itemProvider in
print(itemProvider)
return true
}
}
}
在此示例中,您可以将上面的图像拖到拖放区中,它将打印出提供程序。现在,仅仅更改typeIdentifier 就会破坏一切。
static let sharedTypeIdentifier = "com.procrastin8.plzwork"
struct ReleaseView: View {
// skipping the unchanged bits
.onDrag {
return NSItemProvider(item: "\(self.id)" as NSString, typeIdentifier: sharedTypeIdentifier)
}
}
struct DropZone: View {
// skipping the unchanged bits
.onDrop(of: [sharedTypeIdentifier], isTargeted: self.$isDragging) { itemProvider in
print(itemProvider)
return true
}
}
现在这不起作用。在这里使用相同的常量,所以它不是字符串不匹配。又一个 SwiftUI 错误?
【问题讨论】: