NSWindow 阴影轮廓
我正在通过为窗口设置自定义内容视图来绘制自定义窗口。当我绘制自定义视图时,我给它圆角和漂亮的轮廓来模仿正确的窗口。
然而,我在窗口周围看到另一个 1 像素的轮廓,它偏离了角落的边缘。我发现,如果我关闭阴影,它就会消失,但显然,因为这想要像窗户一样,所以我需要阴影。以下是我对 1px 轮廓的理解:
如何防止这种情况发生?
编辑
用于绘制自定义窗口内容视图的代码:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:5];
NSGradient* aGradient = [[[NSGradient alloc] initWithColorsAndLocations:
[NSColor colorWithDeviceRed:0.5569 green:0.5137 blue:0.4588 alpha:1.0000], 0.0,
[NSColor colorWithDeviceRed:0.5569 green:0.5137 blue:0.4588 alpha:1.0000], 1.0,
nil] autorelease];
[aGradient drawInBezierPath:path angle:90];
[path setLineWidth:4];
[[NSColor colorWithDeviceRed:0.4235 green:0.3922 blue:0.3451 alpha:0.9000] setStroke];
[path strokeInside];
[path setLineWidth:3];
[[NSColor colorWithDeviceRed:0.8431 green:0.8314 blue:0.8078 alpha:1.0000] setStroke];
[path strokeInside];
[path setLineWidth:1];
[[NSColor colorWithDeviceRed:0.4235 green:0.3922 blue:0.3451 alpha:0.9000] setStroke];
[path strokeInside];
I am drawing a custom window by setting a custom content view for the window. When I draw the custom view I give it rounded corners and a nice outline to mimic a proper window.
However, I see another 1 px outline around the window which strays from the edge at the corners. I have found that if I turn off the shadow it goes away, but obviously as this wants to act like a window I need the shadow. Here's what I mean about the 1px outline:
How can I prevent this?
EDIT
Code for drawing the custom window's content view:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:5];
NSGradient* aGradient = [[[NSGradient alloc] initWithColorsAndLocations:
[NSColor colorWithDeviceRed:0.5569 green:0.5137 blue:0.4588 alpha:1.0000], 0.0,
[NSColor colorWithDeviceRed:0.5569 green:0.5137 blue:0.4588 alpha:1.0000], 1.0,
nil] autorelease];
[aGradient drawInBezierPath:path angle:90];
[path setLineWidth:4];
[[NSColor colorWithDeviceRed:0.4235 green:0.3922 blue:0.3451 alpha:0.9000] setStroke];
[path strokeInside];
[path setLineWidth:3];
[[NSColor colorWithDeviceRed:0.8431 green:0.8314 blue:0.8078 alpha:1.0000] setStroke];
[path strokeInside];
[path setLineWidth:1];
[[NSColor colorWithDeviceRed:0.4235 green:0.3922 blue:0.3451 alpha:0.9000] setStroke];
[path strokeInside];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要问我是怎么得到这个的,但这会解决你的问题。
使用以下内容为
NSWindow
定义一个类别:免责声明: 这会覆盖
NSWindow
的内部方法,因此使用它需要您自担风险。它可能会因任何 OS X 更新而中断。Don't ask me how I got this, but this will solve your problem.
Define a category for
NSWindow
with the following content:DISCLAIMER: This overrides the internal method of
NSWindow
, so use it at your own risk. It may break with any OS X update.您需要通过发送
-invalidateShadow
来告诉窗口重新计算其阴影。You need to tell the window to recompute its shadow by sending it
-invalidateShadow
.尝试:
Try:
这条窗口区域轮廓线是自动绘制的。我有一个窗口,这条线在底部圆角周围精确运行。您必须将窗口设置为非不透明,并将背景颜色设置为透明:
contentView -drawRect: 中的某处:您
这样做应该可以。
This line contouring the window area is drawn automatically. I have a window which has this line running accurately around bottom rounded corners. You have to setup the window as non-opaque and the background color to transparent:
The somewhere in the contentView -drawRect: you do
That should work.
据我正确理解,阴影是由 Windows 服务器绘制的。当您绘制带有圆角或其他非矩形形状的自定义 NSWindow 时,窗口服务器不会计算这些透明像素,也不会在它们下方放置阴影。
我开发了一些 hack 来避免这种行为。只需在路径下放置额外的阴影,如下所示:
理想情况下,为了获得完美的结果,我认为阴影必须等于窗口服务器。
As I understand correctly, shadows are drawn by windows server. When you draw custom NSWindow with rounded corners or other not rectangular shapes, window server don't count those transparent pixels and dont drop shadow under them.
I developed some hack to avoid such behavior. Just drop additional shadow under your path, something like this:
Ideally for perfect result i fink shadow must be equal to window servers.