我想绘制一个形状,在它上面我想绘制较小的透明形状。
-(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];
}
我也尝试过使用 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 后面的东西。 (如果你填充椭圆形,在……呃……窗口中给自己一个更大的窗口,效果会更好。)
(为什么轨道看起来像充满了灰色?那是您在那里看到的窗口的阴影。当您真正尝试时,您将能够通过轨道看到系统上的其他窗口,如下所示你移动窗口。)
关于objective-c - 设置复合绘图模式导致 NSWindow 上出现黑洞/补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918698/