NSView 投影使用 setShadow:
我正在尝试为自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答问题之前的一些注意事项:
NSView
上调用super
的drawRect:
实现。默认实现不执行任何操作。[selfbounds]
作为填充矩形,而不是dirtyRect
。dirtyRect
参数用于指示视图中需要绘制的部分,仅用于绘制优化。dropShadow
对象。您应该在创建后对其调用autorelease
或在调用setShadow:
后对其调用release
。阴影不显示的原因有两个。首先,为了让图层支持的视图显示阴影,视图的父视图也必须是图层支持的。
其次,您要设置阴影的颜色,但不设置其他参数:
A few notes before answering the question:
super
's implementation ofdrawRect:
on a vanillaNSView
. The default implementation does nothing.[self bounds]
as the fill rectangle, notdirtyRect
. ThedirtyRect
parameter is used to indicate the part of the view that needs drawing and is used for drawing optimisation only.dropShadow
object. You should either callautorelease
on it after creation or callrelease
on it after callingsetShadow:
.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: