NSView 投影使用 setShadow:

发布于 2024-12-29 17:21:03 字数 629 浏览 4 评论 0原文

我正在尝试为自定义 NSView 子类制作阴影。

到目前为止,我已经做到了:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSShadow *dropShadow = [[NSShadow alloc] init];
        [dropShadow setShadowColor: [NSColor redColor]];

        [self setWantsLayer: YES];
        [self setShadow: dropShadow];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor blueColor] setFill];
    NSRectFill(dirtyRect);

    [super drawRect: dirtyRect];
}

它只渲染一个蓝色方块(即没有阴影)。

我是否将阴影设置在正确的位置? 我是否满足使用 setShadow: 的所有必要要求?

I'm attempting to make a drop shadow for a custom NSView subclass.

So far, I've managed:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSShadow *dropShadow = [[NSShadow alloc] init];
        [dropShadow setShadowColor: [NSColor redColor]];

        [self setWantsLayer: YES];
        [self setShadow: dropShadow];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor blueColor] setFill];
    NSRectFill(dirtyRect);

    [super drawRect: dirtyRect];
}

which only renders a blue square (i.e. no shadow).

Am I setting up the drop shadow in the right place?
Am I meeting all of the necessary requirements for the use of setShadow:?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-01-05 17:21:03

回答问题之前的一些注意事项:

  • 您不需要在普通的 NSView 上调用 superdrawRect: 实现。默认实现不执行任何操作。
  • 您应该使用[selfbounds]作为填充矩形,而不是dirtyRectdirtyRect参数用于指示视图中需要绘制的部分,仅用于绘制优化。
  • 您正在泄漏 dropShadow 对象。您应该在创建后对其调用 autorelease 或在调用 setShadow: 后对其调用 release

阴影不显示的原因有两个。首先,为了让图层支持的视图显示阴影,视图的父视图也必须是图层支持的。

其次,您要设置阴影的颜色,但不设置其他参数:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSShadow *dropShadow = [[NSShadow alloc] init];
        [dropShadow setShadowColor:[NSColor redColor]];
        [dropShadow setShadowOffset:NSMakeSize(0, -10.0)];
        [dropShadow setShadowBlurRadius:10.0];

        [self setWantsLayer: YES];
        [self setShadow: dropShadow];

        [dropShadow release];
    }

    return self;
}

A few notes before answering the question:

  • You don't need to call super's implementation of drawRect: on a vanilla NSView. The default implementation does nothing.
  • You should be using [self bounds] as the fill rectangle, not dirtyRect. The dirtyRect parameter is used to indicate the part of the view that needs drawing and is used for drawing optimisation only.
  • You are leaking the dropShadow object. You should either call autorelease on it after creation or call release on it after calling setShadow:.

The reason that the shadow isn't displaying are twofold. Firstly, in order for layer-backed views to display a shadow, the view's superview must also be layer-backed.

Secondly, you're setting the shadow's color but not its other parameters:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSShadow *dropShadow = [[NSShadow alloc] init];
        [dropShadow setShadowColor:[NSColor redColor]];
        [dropShadow setShadowOffset:NSMakeSize(0, -10.0)];
        [dropShadow setShadowBlurRadius:10.0];

        [self setWantsLayer: YES];
        [self setShadow: dropShadow];

        [dropShadow release];
    }

    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文