在NSWindow上设置复合绘图模式,导致黑洞/补丁

时间:2013-02-17 06:48:25

标签: objective-c cocoa drawing quartz-2d

我想绘制一个形状,在它上面我想绘制较小的透明形状。

-(void) drawRect:(NSRect)dirtyRect
{
    //clear everything
    {
        [[NSColor whiteColor] set];
        [[NSBezierPath bezierPathWithRect:dirtyRect] fill];
    }


    NSBezierPath    *thePath = [NSBezierPath bezierPathWithOvalInRect: self.bounds];
    [thePath setLineWidth: 30];
    [[NSColor blueColor] set];
    [thePath stroke];

    [[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeCopy];
    [[NSColor clearColor] set];
    [thePath setLineWidth: 4];
    [thePath stroke];
}

As a result of above program I'm getting the Blue Oval and a black Oval at the center of blue Oval.

我也尝试过NSImage forst然后再观看,但仍然得到相同的结果。

-(void) drawRect:(NSRect)dirtyRect
{
    //clear everything
    {
        [[NSColor whiteColor] set];
        [[NSBezierPath bezierPathWithRect:dirtyRect] fill];
    }


    NSImage* image = [[NSImage alloc] initWithSize:self.frame.size];

    [image lockFocus];

    //clear everything
    {
        [[NSColor whiteColor] set];
        [[NSBezierPath bezierPathWithRect:dirtyRect] fill];
    }

    NSBezierPath    *thePath = [NSBezierPath bezierPathWithOvalInRect: self.bounds];
    [thePath setLineWidth: 30];
    [[NSColor blueColor] set];
    [thePath stroke];

    [[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeCopy];
    [[NSColor clearColor] set];
    [thePath setLineWidth: 4];
    [thePath stroke];

    [image unlockFocus];

    [image drawInRect:self.frame fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
}

1 个答案:

答案 0 :(得分:2)

椭圆形显示为黑色,因为默认情况下窗口是不透明的。您已成功在窗口中切割出椭圆形轨道,但由于窗口是不透明的,没有工业激光可以在显示器中切割相同的椭圆形,因此Mac必须在那里显示一些颜色。它显示的颜色是clearColor的颜色:黑色。

解决方案是将窗口opaque设置为NO

默认值为YES,这是好的和有效的,但(实际上,因为)它会阻止其他窗口的内容显示。将其设置为NO将允许您通过细椭圆形轨道查看窗口后面的内容。 (如果填充椭圆形,它会更好用,在......窗口中给自己一个更大的窗口。)

This is how it looks with the window's opaque turned off.

(为什么轨道看起来像灰色?这是你在那里看到的窗户的阴影。当你真实地尝试它时,你将能够通过轨道看到你系统上的其他窗口你移动窗户。)