简体   繁体   中英

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. 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.

Is it possible to draw something like this inside a NSView , and if so: how do I do it? 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:

- (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.

This solution will also scale your image to fit the size of the view it is inserted into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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