Swift OSX NSImageView 拖放

Swift OSX NSImageView Drag and Drop

我试图在拖放图像后执行操作 NSImageView 但它不起作用。如何控制拖放操作?

我有一个 logoFornecedorImageView 插座,它是一个 NSImageView 插座。我的 class 继承自 NSDraggingDestinatio 并注册了拖动的类型,但是当我 运行 软件并在其上拖动图像时没有任何反应,控制台中没有打印任何内容。

import Cocoa

class InserirFornecedorViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSDraggingDestination {

    @IBOutlet weak var tituloJanelaLabel: NSTextField!
    @IBOutlet weak var logoFornecedorImageView: NSImageView!
    @IBOutlet weak var nomeFornecedorTextField: NSTextField!
    @IBOutlet weak var materialFornecidoTextField: NSTextField!
    @IBOutlet weak var materiaisTableView: NSTableView!
    @IBOutlet weak var indicadorAtividadeProgressIndicator: NSProgressIndicator!
    @IBOutlet weak var salvarFornercedorButton: NSButton!

    var fornecedor: Fornecedor?

    var logoFornecedorSelecionada = false

    override func viewDidLoad() {

        super.viewDidLoad()

        materiaisTableView.dataSource = self
        materiaisTableView.delegate = self

        logoFornecedorImageView.register(forDraggedTypes: logoFornecedorImageView.registeredDraggedTypes)

        fornecedor = Fornecedor()
    }

    func draggingEnded(_ sender: NSDraggingInfo?) {

        print("END")
        logoFornecedorSelecionada = true
    }

    func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {

        print("ENTERED")
        return .generic
    }

    func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {

        print("UPDATED")
        return .generic
    }

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

        return true
    }

    func numberOfRows(in tableView: NSTableView) -> Int {

        return fornecedor?.materiais.count ?? 0
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        var cell: NSTableCellView?

        //if tableColumn == tableView.tableColumns[0]

        if fornecedor?.materiais.count != 0 {
            let identificadorCell = "materialCellView"

            let material = fornecedor?.materiais[row]

            cell = tableView.make(withIdentifier: identificadorCell, owner: nil) as? NSTableCellView

            cell?.textField?.stringValue = material!
        }

        return cell
    }

    @IBAction func selecionarImagemButtonClicked(_ sender: NSButton) {

        let panel = NSOpenPanel()
        panel.canChooseFiles = true
        panel.canChooseDirectories = false
        panel.allowsMultipleSelection = false
        panel.canCreateDirectories = false
        //panel.allowedFileTypes = ["jpg","png","pct","bmp", "tiff"]
        panel.allowedFileTypes = NSImage.imageTypes()

        panel.beginSheetModal(for: view.window!) { (result) in

            if result == NSFileHandlingPanelOKButton {
                self.logoFornecedorImageView.image = NSImage(byReferencing: panel.url!)

                self.logoFornecedorSelecionada = true
            }
        }
    }

    @IBAction func removerImagemButtonClicked(_ sender: NSButton) {

        logoFornecedorImageView.image = NSImage(named: "LogoImagemTexto")

        logoFornecedorSelecionada = false
    }

    @IBAction func adicionarMaterialButton(_ sender: NSButton) {

        if materialFornecidoTextField.stringValue.isEmpty {
            mostrarErro(mensagem: "Erro de preenchimento", informativo: "Informe o material")
            materialFornecidoTextField.becomeFirstResponder()
        } else {
            fornecedor?.materiais.append(materialFornecidoTextField.stringValue)

            materialFornecidoTextField.stringValue = ""

            fornecedor?.materiais.sort {
                [=12=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending
            }
            materiaisTableView.reloadData()
        }
    }

    @IBAction func voltarButton(_ sender: NSButton) {

        //let usarSoftViewController = presenting as! UsarSoftViewController
        //usarSoftViewController.ativarBoxPrincipal()
        //usarSoftViewController.usuario = usuario
        //usarSoftViewController.fazerLogin()

        dismiss(self)
    }

    func mostrarErro(mensagem: String, informativo: String) {

        let alert = NSAlert()
        alert.messageText = mensagem
        alert.informativeText = informativo
        alert.addButton(withTitle: "Fechar")
        alert.alertStyle = .critical
        alert.runModal()
    }
}

谢谢大家

我刚刚执行了一个动作,效果很好。

@IBAction func logoFornecedorImageDropped(_ sender: NSImageView) {

        self.logoFornecedorSelecionada = true
    }

为了更清楚地说明这一点,您可以将常规 IBAction 连接到 Editable NSImageView 并获取图像。您不必进行任何子类化或拖动委托。

只需这样做:

@IBAction func imageChange(_ sender: NSImageView) {
  if let image = sender.image{
    let imageData = image.tiffRepresentation 
  }
}

希望对您有所帮助。 :)