Hello, I created a subclass of CALayer. But the Software isn't behaving as I expect it, maybe someone can explain to me what I am misunderstanding.
The subclass inherits a function changecolor()
which gets called just for test 1s after start within a timer callback. The function currently just sets the background color of the layer to a different value.
If I do this, I get the following error:
Test3/ViewController.swift:62: Fatal error: Use of unimplemented initializer 'init(layer:)' for class 'Test3.timeRuler'
I have to implement
override init(layer: Any) { super.init(layer: layer) }
to get it working. Then the Layers color is updated as expected. BUT WHAT I EXPECTED:
I expected that a change in background color does not need a reinitialization of the layer, I expected that the change first does nothing, and the layer gets redrawn if I set the layers setNeedsDisplay()
.
The draw and display functions are never called somehow. They are however called if I call setNeedsDisplay()
but the color changes anyway if override init(layer: Any)
is defined.
What am I missing here?
class timeRuler :CALayer{ override init() { super.init(); frame = NSRect(x: 0, y: 0, width: 300, height: 300); backgroundColor=NSColor.blue.cgColor; } func changecolor(){ backgroundColor=NSColor.purple.cgColor; //setNeedsDisplay() } /* override init(layer: Any) { super.init(layer: layer) }*/ override func draw(in ctx: CGContext) { super.draw(in: ctx); print("layer draw update"); } override func display() { //backgroundColor=NSColor.blue.cgColor; super.display(); print("layer update"); } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }