重新绘制使用-setClip方法在自定义视图上插入NSShadow [英] Redrawed inset NSShadow on a Custom View using -setClip method
问题描述
我有奇怪的问题,与这个问题的答案有关:
I have and odd problem, related with the answer of this question:
我使用这个代码到drawRect方法中自定义视图。我有这样的:
I use this code into the drawRect method of a custom view. I have exactly this:
- (void)drawRect:(NSRect)rect
{
// Create and fill the shown path
NSBezierPath *path = [NSBezierPath
bezierPathWithRoundedRect:[self bounds]
xRadius:4.0f
yRadius:4.0f];
[[NSColor colorWithCalibratedWhite:0.8f alpha:0.2f] set];
[path fill];
// Save the graphics state for shadow
[NSGraphicsContext saveGraphicsState];
// Set the shown path as the clip
[path setClip];
// Create and stroke the shadow
NSShadow * shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0f alpha:0.8f]];
[shadow setShadowBlurRadius:2.0];
[shadow set];
[path stroke];
// Restore the graphics state
[NSGraphicsContext restoreGraphicsState];
if ( highlight && [[self window] firstResponder] == self ) {
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:[self bounds]] fill];
}
}
问题出现在我添加一个多行标签兄弟姐妹或我的自定义视图的子项)。
The problem comes when I add a Multiline Label (either sibling or child of my custom view).
当我的程序窗口失去焦点,我回到它,我的内阴影/似乎阴影叠加。这很奇怪,因为如果我的窗口只有这个自定义视图,它会很好。
When my program window loses the focus and I come back to it, my inner shadow / stroke go darker. It seems that the shadows superimpose. It's strange because as said, if my window only have this custom view, it goes well.
如果我评论行
[path setClip];
阴影不再叠加,但我没有得到圆角的期望效果类似于NSBox)。
the shadow isn't superimposed anymore, but I don't get the desired effect of rounded corners (similar than NSBox).
我尝试使用按钮而不是多行标签发生了什么,通过失去/获取窗口焦点没有问题,但是当我
I tried what happens with a Push Button instead of a Multiline Label, and by losing / getting the window focus it has no problems, but when I click the button the shadow gets superimposed.
我发现问题与这里类似,但在Cocoa而不是Java:
I find the problem is similar than here, but in Cocoa instead of Java:
感谢您的帮助!
推荐答案
您不应该使用 -setClip
,除非你知道你在做什么。您应该使用 -addClip
,它尊重现有的剪切路径。
You should never use -setClip
unless you know what you're doing. You should use -addClip
instead, which respects the existing clipping paths.
这篇关于重新绘制使用-setClip方法在自定义视图上插入NSShadow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!