【问题标题】:How Do I Add Other Pasteboard Types to an HFS Promise Drag in Cocoa?如何将其他粘贴板类型添加到 Cocoa 中的 HFS Promise 拖动?
【发布时间】:2016-10-02 02:56:14
【问题描述】:
我正在尝试在我的 Mac 应用程序中启动拖累。我希望拖动提供原生 UTI 和文件承诺,以便用户可以将剪辑拖动到桌面。
根据苹果的obsolete documentation,实现的方法是:
用dragPromisedFilesOfTypes:fromRect:source:slideBack:event:开始“承诺”拖动
通过覆盖dragImage:at:offset:event:pasteboard:source:slideBack:添加其他粘贴板类型
问题在于 Apple 已经用 beginDraggingSession: 替换了 AppKit 的 dragImage: 方法......而 dragPromisedFilesOfTypes: 似乎没有调用它。
现在最好的方法是什么?
【问题讨论】:
标签:
macos
cocoa
drag-and-drop
appkit
【解决方案1】:
所以看起来你必须自己设置承诺。例如:
let writer = NSPasteboardItem()
// We can provide "MP3" data, and/or a "File promise"
writer.setDataProvider(
data_source,
forTypes: [ kUTTypeMP3, kPasteboardTypeFileURLPromise]
)
// If the receiver wants the "File promise", we'll
// be writing a "CAF file" for them
writer.setString( AVFileTypeCoreAudioFormat, forType: kPasteboardTypeFilePromiseContent )
let drag_item = NSDraggingItem( pasteboardWriter: writer )
let drag_session = self.beginDraggingSession( with: [drag_item], event: event, source: self )
在这个例子中,我设置了一个可以立即提供 MP3 的普通拖动,或者一个 CAF 文件的承诺。
通过以这种方式开始拖动,“namesOfPromisedFilesDropped:”被调用,就像“dragPromisedFilesOfTypes:”一样,但我们也可以设置非承诺内容。
编辑:感谢 jnadeau 指出 macOS 10.12 添加了“NSFilePromiseProvider”,这可能更简单。我需要支持 10.10 和 10.11,但我提一下,以防其他人觉得它有用。