Redrawed inset NSShadow on a Custom View using -setClip method

和自甴很熟 提交于 2019-12-22 13:59:10

问题


I have and odd problem, related with the answer of this question:

Draw an Inset NSShadow and Inset Stroke

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.

If I comment the line

[path setClip]; 

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.

I find the problem is similar than here, but in Cocoa instead of Java:

Java setClip seems to redraw

Thanks for your help!


回答1:


You should never use -setClip unless you know what you're doing. You should use -addClip instead, which respects the existing clipping paths.



来源:https://stackoverflow.com/questions/8535613/redrawed-inset-nsshadow-on-a-custom-view-using-setclip-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!