objective-c - 设置复合绘图模式导致 NSWindow 上出现黑洞/补丁

标签 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];
}

最佳答案

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

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

默认值为YES,这很好且高效,但(实际上,因为)它会阻止其他窗口的内容显示。将其设置为NO将允许您通过细椭圆形轨道看到 window 后面的东西。 (如果你填充椭圆形,在……呃……窗口中给自己一个更大的窗口,效果会更好。)

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

(为什么轨道看起来像充满了灰色?那是您在那里看到的窗口的阴影。当您真正尝试时,您将能够通过轨道看到系统上的其他窗口,如下所示你移动窗口。)

关于objective-c - 设置复合绘图模式导致 NSWindow 上出现黑洞/补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918698/

相关文章:

iphone - NS_BLOCK_ASSERTIONS 是否同时禁用 NSAssert 和 assert() 调用?

ios - AFNetworking 500响应主体

objective-c - 如何用动画淡出 NSView?

iphone - 使用 NSInspiration : constant value

算法:记住绘图

ios - 数据未写入文件?

objective-c - 用于 Objective-C 的 REPL

objective-c - 包含正斜杠的 URL 不适用于 NSWorkSpace

drawing - 使用 Xlib 启用抗锯齿

C#:绘制自己的条形图