我有一个奇怪的问题,与这个问题的答案有关:
我将此代码用于自定义视图的 drawRect 方法。我有这个:
- (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];
}
}
当我添加一个多行标签(我的自定义视图的同级或子级)时,问题就出现了。
当我的程序窗口失去焦点并且我回到它时,我的内部阴影/笔画变暗。似乎阴影叠加了。这很奇怪,因为如前所述,如果我的窗口只有这个自定义视图,它会很顺利。
如果我评论该行
[path setClip];
阴影不再叠加,但我没有得到想要的圆角效果(类似于 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.
I find the problem is similar than here, but in Cocoa instead of Java:
Thanks for your help!