swfit macos 拖拽问题

问与答 · suifengqjn · 发布于 6 个月前 · 730 次阅读

具体需求是这样的,我要实现一个拖入文件到框内,获取文件路径的功能。我在performDragOperation方法里获取不到文件路径

class DragView: NSView {
    var delegate : DragViewDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
    self.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL,NSPasteboard.PasteboardType.tiff,NSPasteboard.PasteboardType.URL,NSPasteboard.PasteboardType.string])

    }


    override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
        if let delegate = self.delegate {
            delegate.dragEnter()
        }
        return NSDragOperation.generic
    }

    override func draggingExited(_ sender: NSDraggingInfo?) {
        if let delegate = self.delegate {
            delegate.dragExit()
        }
    }

    override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {

        return true
    }


    override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {

        var filePath = ""
        if let files = sender.draggingPasteboard().propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? NSArray {
            // 这里我试了 .string .URL .fileContents 等,每种属性都是空的
            for path in files {
                filePath = path as! String
            }
        }

        //NSPasteboard.PasteboardType
        var draggedFilenames = sender.draggingPasteboard().propertyList(forType: NSPasteboard.PasteboardType.fileContents)
        if let delegate = self.delegate {
            delegate.dragFileOk(filePath: filePath)
        }
        return true
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
    }

}
共收到 0 条回复
回帖