Create Drop Shadow for NSView - Cocoa

﹥>﹥吖頭↗ 提交于 2019-12-08 04:39:12

问题


I'm trying to create a drop shadow surrounding the NSView like how NSWindow does it with its shadow, but I'm having some difficulty. I created a class for the NSView I'm creating the drop shadow for and I'm using this code for the overriding method:

   -(void)drawRect:(NSRect)dirtyRect {
    NSRect rect = NSInsetRect([self bounds], 10.0, 10.0);
    NSShadow *dropShadow = [[[NSShadow alloc] init] autorelease];
    [dropShadow setShadowColor:[NSColor blackColor]];
    [dropShadow setShadowBlurRadius:5];
    [dropShadow setShadowOffset:NSMakeSize(0,-3)];

    [NSGraphicsContext saveGraphicsState];

    [dropShadow set];

    NSRectFill(rect);

    [NSGraphicsContext restoreGraphicsState];

    [super drawRect:dirtyRect];
}

This doesn't really create a drop shadow in which I'm looking.

Here is the shadow I'm trying to aim for...

rather creates a line through the NSView that seems like a border within the bounds of the view. Anyone got any ideas for this?


回答1:


I have faced similar shadow issues because NSView clips its bounds.

I fixed it when I used a layer backed view. I simply set the superview's wantsLayer property to YES.. i.e [[view superView] setWantsLayer:YES] and set shadow for view [view setShadow:dropShadow].



来源:https://stackoverflow.com/questions/6791116/create-drop-shadow-for-nsview-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!