Swift: Opening a file by drag-and-drop in window

后端 未结 3 2014
傲寒
傲寒 2021-02-06 10:49

In Swift, how can I build an area in a window of my Mac app where a user can drag-and-drop a folder onto this area, and have my app receive the path of the folder?

In pr

相关标签:
3条回答
  • 2021-02-06 11:26

    In a custom NSView:

    override init(frame frameRect: NSRect)
    {
        super.init(frame: frameRect)
        registerForDraggedTypes([kUTTypeFileURL,kUTTypeImage])
    }
    
    override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation
    {
        println("dragging entered")
        return NSDragOperation.Copy
    }
    

    The catch is, to get it working I had to put that custom view as a leaf and the last view in the storyboard

    0 讨论(0)
  • 2021-02-06 11:29

    This worked for me (on a NSWindow subclass):

    In awakeFromNib:

    registerForDraggedTypes([NSFilenamesPboardType])
    

    Then add the following operations (at least draggingEntered and performDragOperation) to the window (or view):

    func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {
        let sourceDragMask = sender.draggingSourceOperationMask()
        let pboard = sender.draggingPasteboard()!
        if pboard.availableTypeFromArray([NSFilenamesPboardType]) == NSFilenamesPboardType {
            if sourceDragMask.rawValue & NSDragOperation.Generic.rawValue != 0 {
                return NSDragOperation.Generic
            }
        }
        return NSDragOperation.None
    }
    
    func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation {
        return NSDragOperation.Generic
    }
    
    func prepareForDragOperation(sender: NSDraggingInfo) -> Bool {
        return true
    }
    
    func performDragOperation(sender: NSDraggingInfo) -> Bool {
       // ... perform your magic
       // return true/false depending on success
    }
    
    0 讨论(0)
  • 2021-02-06 11:40

    my 2 cents for OSX - swift 5 (and fixed for loading from XIBs/storybord.)

    //  Created by ing.conti on 31th jan 2020.
    //
    
    import Cocoa
    
    class AnalysisView: NSView {
    
        override init(frame frameRect: NSRect)
        {
            super.init(frame: frameRect)
            self.registerMyTypes()
        }
    
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            self.registerMyTypes()
        }
    
    
        final private func registerMyTypes()
        {
            registerForDraggedTypes(
                [NSPasteboard.PasteboardType.URL,
                 NSPasteboard.PasteboardType.fileURL,
                 NSPasteboard.PasteboardType.png,
                 NSPasteboard.PasteboardType.fileNameType(forPathExtension: "wtf")
            ])
        }
    
    
    
        override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
            print("draggingEntered")
            return NSDragOperation.copy
        }
    
        override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
            let allow = true // check your types...
            print("prepareForDragOperation")
            return allow
        }
    
        override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
    
            let pasteBoard = sender.draggingPasteboard
            print("performDragOperation")
    
            if let urls = pasteBoard.readObjects(forClasses: [NSURL.self]) as? [URL]{
                // consume them...
                print(urls)
                return true
            }
            return false
        }
    }
    
    0 讨论(0)
提交回复
热议问题