为NSView创建阴影-Cocoa [英] Create Drop Shadow for NSView - Cocoa

查看:274
本文介绍了为NSView创建阴影-Cocoa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕NSView创建一个投影,就像NSWindow对其阴影进行处理一样,但是我遇到了一些困难.我为NSView创建了一个类,并为其创建了阴影,并且我将此代码用于覆盖方法:

   -(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];
}

这并没有真正创建我正在寻找的阴影.

这是我要瞄准的阴影...

宁可在NSView中创建一条线,该线看起来像视图边界内的边界.有人对此有任何想法吗?

解决方案

由于NSView限制了边界,我也遇到了类似的阴影问题.

当我使用支持图层的视图时,我对其进行了修复.我只是将超级视图的wantsLayer属性设置为YES.即 [[view superView] setWantsLayer:YES] 并为视图 [view setShadow:dropShadow] 设置阴影./p>

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?

解决方案

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

这篇关于为NSView创建阴影-Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆