0
我有一個層支持(不層託管)的子類的NSView稱爲MetalView:的drawRect:不呼籲的NSView與CAMetalLayer
import Cocoa
import Metal
class MetalView: NSView {
// MARK: Properties
var metalLayer: CAMetalLayer {
return layer as! CAMetalLayer
}
// MARK: - Overrides
override func awakeFromNib() {
super.awakeFromNib()
initialize()
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
render()
}
override func drawLayer(layer: CALayer, inContext ctx: CGContext) {
super.drawLayer(layer, inContext: ctx)
render()
}
override func displayLayer(layer: CALayer) {
super.displayLayer(layer)
render()
}
override func makeBackingLayer() -> CALayer {
return CAMetalLayer()
}
override var wantsUpdateLayer: Bool {
return false
}
// MARK: - Initialization
func initialize() {
// Layer
wantsLayer = true
metalLayer.delegate = self
metalLayer.device = MTLCreateSystemDefaultDevice()
metalLayer.pixelFormat = .BGRA8Unorm
metalLayer.framebufferOnly = true
}
// MARK: - Rendering
func render() {
// Rendering
metalLayer.frame = frame
NSLog("rendering")
}
}
的渲染方法沒有被調用。我試過設置wantsUpdateLayer到true。該渲染方法被調用與我刪除了這部分:
wantsLayer = true
metalLayer.delegate = self
metalLayer.device = MTLCreateSystemDefaultDevice()
metalLayer.pixelFormat = .BGRA8Unorm
metalLayer.framebufferOnly = true
我試着設置'wantsUpdateLayer'到TRUE;並覆蓋'updateLayer',但它也不管用。我也嘗試將設置圖層的代碼移動到'makeBackingLayer'中。 –
@GorGyolchanyan在設置wantsUpdateLayer之後,當你調用setNeedsDisplay時,updateLayer不會被調用? –
我已經手動調用了'metalLayer.setNeedsDisplay()',並且它只會導致調用displayLayer(layer:CALayer)方法,而不管使用'wantsUpdateLayer'。問題是它沒有被調用來調整視圖的大小。 –