Macos 为什么当我勾选CoreAnimationLayer启用时,背景色不能是透明的?

Macos 为什么当我勾选CoreAnimationLayer启用时,背景色不能是透明的?,macos,cocoa,swift,core-animation,Macos,Cocoa,Swift,Core Animation,我有一个窗口和一个视图。在NSView中,我覆盖了drawRect方法,如下所示: override func drawRect(dirtyRect: NSRect) { NSColor.clearColor().set() NSRectFill(self.frame) self.layer?.backgroundColor = NSColor.clearColor().CGColor let context = NSGraphicsContext.curren

我有一个窗口和一个视图。在NSView中,我覆盖了
drawRect
方法,如下所示:

override func drawRect(dirtyRect: NSRect) {
    NSColor.clearColor().set()
    NSRectFill(self.frame)

    self.layer?.backgroundColor = NSColor.clearColor().CGColor
    let context = NSGraphicsContext.currentContext()?.CGContext
    CGContextSetStrokeColorWithColor(context, NSColor.redColor().CGColor)
    CGContextSetLineWidth(context, 200.0)
    CGContextMoveToPoint(context, 0.0, 0.0) //start at this point
    CGContextAddLineToPoint(context, 500, 1000) //draw to this point

    // and now draw the Path!
    CGContextStrokePath(context)
 }
override init(contentRect: NSRect, styleMask aStyle: Int, backing bufferingType: NSBackingStoreType, defer flag: Bool) {

    super.init(contentRect: contentRect, styleMask: aStyle, backing: NSBackingStoreType.Buffered, defer: false)
    self.alphaValue = 1
    self.opaque = false
}
NSWindow的实现如下所示:

override func drawRect(dirtyRect: NSRect) {
    NSColor.clearColor().set()
    NSRectFill(self.frame)

    self.layer?.backgroundColor = NSColor.clearColor().CGColor
    let context = NSGraphicsContext.currentContext()?.CGContext
    CGContextSetStrokeColorWithColor(context, NSColor.redColor().CGColor)
    CGContextSetLineWidth(context, 200.0)
    CGContextMoveToPoint(context, 0.0, 0.0) //start at this point
    CGContextAddLineToPoint(context, 500, 1000) //draw to this point

    // and now draw the Path!
    CGContextStrokePath(context)
 }
override init(contentRect: NSRect, styleMask aStyle: Int, backing bufferingType: NSBackingStoreType, defer flag: Bool) {

    super.init(contentRect: contentRect, styleMask: aStyle, backing: NSBackingStoreType.Buffered, defer: false)
    self.alphaValue = 1
    self.opaque = false
}
以下是模拟结果:


彩色背景是透明的墙纸。所以,我能看穿背景。但当我在NSView中打开
核心动画层
时,会发生以下情况。它变成灰色,这不是我所期望的。发生了什么事?谢谢

需要更多的信息。图形视图是窗口的contentView吗?如果不是,窗口的contentView-WantLayer==是吗?我不完全确定这一点,但您可以尝试至少注释出设置视图根层背景颜色的行。