I try coding simple stuff related to canvas animations. My goal is to create some animations manually on NSView (for example continuously rotating rectangle, no Core Animation framework allowed with between states transitions).
My approach is:
CGContext
.display()
each 1/30 secThe problem is I'm not quite sure that it's the right approach as it creates too much overhead on CPU. I wonder if there is any other right way to reach the same result?
Found some information about needsDisplay
and setNeedsDisplay
to schedule draw
call but it doesn't work when I call it like this "tick" message printed only once and it means that draw
function is called only once
class GraphView: NSView {
...
override func draw(_ dirtyRect: NSRect) {
print("tick")
super.draw(dirtyRect)
let ctx = NSGraphicsContext.current?.cgContext
self.drawdrawPrimitive(context: ctx!)
// tried both but none of them works
self.needsDisplay = true
self.setNeedsDisplay(dirtyRect)
}
}
Found a solution: Apple's SpriteKit handles all updates and does exactly the thing I needed