简体   繁体   English

如何在自定义 NSView 中绘制“拖放”区域

[英]How to draw a "drag and drop" area in a custom NSView

I'm building my first application, in which I have a custom NSView which is used for dragging and dropping files.我正在构建我的第一个应用程序,其中有一个用于拖放文件的自定义NSView I've got the registration of the dragging operation down, but still need to design the drag and drop area, so the users actually know that they can drop files on there.我已经注册了拖拽操作,但是还需要设计拖放区域,让用户真正知道他们可以在那里拖放文件。

I'd like the area to look something like this:我希望该区域看起来像这样:
如何让我的拖放区域看起来像这样?

I've tried some different things out in the drawRect function of my custom NSView class, but so far I haven't been able to even draw a dashed border.我在自定义NSView类的drawRect函数中尝试了一些不同的东西,但到目前为止我什至无法绘制虚线边框。

Is it possible to draw something like this inside a NSView , and if so: how do I do it?是否可以在NSView绘制这样的东西,如果是这样:我该怎么做? Or should I just create a image of how I'd like my drag and drop area to look, and insert this into the view?或者我应该只创建一个我希望我的拖放区域看起来如何的图像,并将其插入到视图中?
Thanks in advance.提前致谢。

If anyone else ever has this issue, the way I solved this was simply inserting an image into the NSView like this:如果其他人遇到过这个问题,我解决这个问题的方法只是像这样将图像插入到NSView

- (void)drawRect:(NSRect)dirtyRect {
    NSImage *image = [NSImage imageNamed:@"Dropzone"];
    [image drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
}

Seems by far to be the easiest way.到目前为止,似乎是最简单的方法。 Especially if you, or someone you know, knows how to do stuff in Photoshop or whatever.特别是如果您或您认识的人知道如何在 Photoshop 或其他任何东西中进行操作。

This solution will also scale your image to fit the size of the view it is inserted into.此解决方案还将缩放您的图像以适应它插入的视图的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM