在NSView中,针对macOS 10.14执行此操作:
override var wantsUpdateLayer: Bool {
return true
}
override func updateLayer() {
if let context = NSGraphicsContext.current?.cgContext {
// Never enters here
}
else {
// Gives run-time error, invalid context
let path = NSBezierPath()
path.move(to: NSPoint(x: 0, y: 0))
path.line(to: NSPoint(x: 10, y: 10))
path.stroke()
}
}
因此,没有有效的图形上下文,因为在重写func draw(_dirtyRect:NSRect)中存在,我似乎无法获得一个。我应该如何获取有效的图形上下文以在updateLayer中进行绘制?
您不应该参与进来updateLayer
,这就是为什么没有cgContext
传递给您的原因。实施CALayerDelegate
的drawLayer:inContext:
针对。updateLayer
是将位图直接分配给图层的另一种方法,并且仅限于此。
参见:https : //developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/SettingUpLayerObjects/SettingUpLayerObjects.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句