使用-setClip方法重新绘制自定义视图上的插入NSShadow

Jor*_*ost 2 cocoa drawing nsbezierpath nsshadow

我有奇怪的问题,与这个问题的答案有关:

绘制插入NSShadow和插入笔划

我将此代码用于自定义视图的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];
    }
}
Run Code Online (Sandbox Code Playgroud)

当我添加一个多行标签(我的自定义视图的兄弟或孩子)时,会出现问题.

当我的程序窗口失去焦点并且我回到它时,我的内部阴影/笔划变暗.似乎阴影叠加了.这很奇怪,因为如上所述,如果我的窗口只有这个自定义视图,那就顺利了.

如果我评论该行

[path setClip]; 
Run Code Online (Sandbox Code Playgroud)

阴影不再叠加,但我没有得到圆角的预期效果(类似于NSBox).

我尝试使用按钮而不是多行标签发生的事情,并且通过丢失/获得窗口焦点它没有问题,但是当我单击按钮时阴影被叠加.

我发现问题与此类似,但在Cocoa而不是Java中:

Java setClip似乎在重绘

谢谢你的帮助!

Rob*_*ger 6

-setClip除非你知道自己在做什么,否则永远不要使用.您应该使用-addClip,它尊重现有的剪切路径.