我建议您尝试以下方式:
子类化 NSPathControl 类,假设你将它命名为 MyNSPathControl
声明您的 NSPathControlDelegate,并允许拖动操作(根据https://developer.apple.com/documentation/appkit/nspathcontroldelegate)
class DragDelegate : NSObject, NSPathControlDelegate {
func pathControl(_ pathControl: NSPathControl, shouldDrag pathItem: NSPathControlItem, with pasteboard: NSPasteboard) -> Bool {
return true;
}
}
- 将委托设置为 MyNSPathControl
class MyNSPathControl : NSPathControl {
var myDelegate = DragDelegate()
override func awakeFromNib() {
super.awakeFromNib()
self.delegate = myDelegate
}
}
- 声明 MyNSPathControl 的扩展实现 NSDraggingSource
extension DraggableView: NSDraggingSource {
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return .every
}
func draggingSession(_ session: NSDraggingSession, willBeginAt screenPoint: NSPoint) {
}
func draggingSession(_ session: NSDraggingSession, movedTo screenPoint: NSPoint) {
}
func draggingSession(_ session: NSDraggingSession, endedAt screenPoint: NSPoint, operation: NSDragOperation) {
// Here it goes
}
}
拖动到垃圾箱时,我可以在操作参数中使用 NSDragOperation.delete。要访问拖动的内容,您应该从会话访问粘贴板,然后访问其项目。
关于删除本身:我认为您需要手动进行。