如何创建一个 ImageView 作为拖放?

How to create an ImageView as drag and drop?

我想知道如何拖放 ImageView, 我将某个文件拖到 ImageView 中,它会渲染 一个动作。

我知道我可以在脚本编辑器中使用下面的代码并将其另存为 然后应用程序可以将文件拖入其中并执行操作,但想要 知道如何在 ImageView 中完成它。

提前致谢!

on open myItems
    repeat with one_item in myItems
        ---Blablabla        
    end repeat
end open

一个 NSImageView 的父级 class 是 NSControl,所以像任何其他控件一样,它有一个 target/action 你可以指定,对于示例:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property mainWindow : missing value

on run -- Script Editor example
    my performSelectorOnMainThread:"doWindowStuff" withObject:(missing value) waitUntilDone:true
end run

on doWindowStuff() -- window stuff needs to be done on the main thread
    try
        set my mainWindow to makeWindow out of {{0, 0}, {400, 200}} given title:"Drag an image:"
        set imageView to makeImageView out of {{100, 50}, {200, 100}}
        mainWindow's contentView's addSubview:imageView
        mainWindow's makeKeyAndOrderFront:me
    on error errmess
        display alert "Error" message errmess
    end try
end doWindowStuff

to makeWindow out of frame given title:title : "" -- make and return a window
    tell (current application's NSWindow's alloc()'s initWithContentRect:frame styleMask:7 backing:2 defer:yes) -- styleMask of 7 includes title, close and minimize buttons
        if first item of frame is {0, 0} then tell it to |center|()
        its setAutorecalculatesKeyViewLoop:true
        its setTitle:title
        return it
    end tell
end makeWindow

to makeImageView out of frame -- make and return an image view
    tell (current application's NSImageView's alloc()'s initWithFrame:frame)
        its setTarget:me
        its setAction:"imageDragged:"
        its setEditable:true
        its setImageScaling:(current application's NSImageScaleProportionallyUpOrDown)
        return it
    end tell
end makeImageView

on imageDragged:sender
    activate me
    display dialog "An image was dragged into the image view."
end imageDragged:

要执行诸如获取放置图像的文件路径之类的操作,NSImageView 需要被子classed。对于 Xcode 项目,为子 class 添加一个新的 .applescript 文件,并在 IB 的 Identity Inspector 中将其设置为您的 imageView 的 class:

script MyImageView -- NSImageView subclass

    property parent : class "NSImageView"
    property imagePath : missing value

    on draggingEntered:sender
        # additional stuff as desired
        return current application's NSDragOperationCopy
    end draggingEntered:

    on performDragOperation:sender
        set pasteboard to sender's draggingPasteboard()
        set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
        set imageURLs to pasteboard's readObjectsForClasses:{current application's |NSURL|} options:theOptions
        set draggedURL to first item of (imageURLs as list)
        set my imagePath to draggedURL as text
        return true -- data accepted
    end performDragOperation:

end script

您还应该在 AppDelegate 的设置中为您的 imageView 注册可接受的拖动类型:

imageView's registerForDraggedTypes:{"public.image"}

当您将某些内容拖到视图中时,文件路径将在 imageView 的 imagePath 属性 中可用。有关详细信息,请参阅 Apple Drag and Drop Programming Topics