我最近开始使用Swift来构建OS X应用程序,我想知道如何实现拖放区域。
更具体地说,我构建了一个处理图像的应用程序,但目前,用户必须手动输入输入图像的路径或使用文件选择器(这很烦人)。我想改进我的应用程序,允许用户通过简单的拖放输入图像(我只需要检索表示图像路径的String即可)。
我怎样才能做到这一点?
这是我在应用程序中使用的示例。
NSDraggingDestination
NSImageView
NSFilenamesPboardType
registerForDraggedTypes
draggingEntered
draggingUpdated
performDragOperation
NSDragOperation
draggingPasteboard
在我的示例中,我添加了一个函数来检查文件扩展名是否在我们想要的扩展名之中。
迅捷2
class MyImageView: NSImageView { override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) } required init?(coder: NSCoder) { super.init(coder: coder) // Declare and register an array of accepted types registerForDraggedTypes([NSFilenamesPboardType, NSURLPboardType, NSPasteboardTypeTIFF]) } let fileTypes = ["jpg", "jpeg", "bmp", "png", "gif"] var fileTypeIsOk = false var droppedFilePath: String? override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation { if checkExtension(sender) { fileTypeIsOk = true return .Copy } else { fileTypeIsOk = false return .None } } override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation { if fileTypeIsOk { return .Copy } else { return .None } } override func performDragOperation(sender: NSDraggingInfo) -> Bool { if let board = sender.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray, imagePath = board[0] as? String { // THIS IS WERE YOU GET THE PATH FOR THE DROPPED FILE droppedFilePath = imagePath return true } return false } func checkExtension(drag: NSDraggingInfo) -> Bool { if let board = drag.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray, path = board[0] as? String { let url = NSURL(fileURLWithPath: path) if let fileExtension = url.pathExtension?.lowercaseString { return fileTypes.contains(fileExtension) } } return false } }
迅捷3
class MyImageView: NSImageView { override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) } required init?(coder: NSCoder) { super.init(coder: coder) // Declare and register an array of accepted types register(forDraggedTypes: [NSFilenamesPboardType, NSURLPboardType, NSPasteboardTypeTIFF]) } let fileTypes = ["jpg", "jpeg", "bmp", "png", "gif"] var fileTypeIsOk = false var droppedFilePath: String? override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { if checkExtension(drag: sender) { fileTypeIsOk = true return .copy } else { fileTypeIsOk = false return [] } } override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation { if fileTypeIsOk { return .copy } else { return [] } } override func performDragOperation(_ sender: NSDraggingInfo) -> Bool { if let board = sender.draggingPasteboard().propertyList(forType: "NSFilenamesPboardType") as? NSArray, imagePath = board[0] as? String { // THIS IS WERE YOU GET THE PATH FOR THE DROPPED FILE droppedFilePath = imagePath return true } return false } func checkExtension(drag: NSDraggingInfo) -> Bool { if let board = drag.draggingPasteboard().propertyList(forType: "NSFilenamesPboardType") as? NSArray, path = board[0] as? String { let url = NSURL(fileURLWithPath: path) if let fileExtension = url.pathExtension?.lowercased() { return fileTypes.contains(fileExtension) } } return false } }