Last active
April 6, 2018 17:41
-
-
Save usagimaru/46118ef276a691f74ddbb097b0f25cd5 to your computer and use it in GitHub Desktop.
Optimize NSView's drawRect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyView: NSView { | |
override func draw(_ dirtyRect: NSRect) { | |
super.draw(dirtyRect) | |
guard let ctx = NSGraphicsContext.current?.cgContext else {return} | |
var rects: UnsafePointer<NSRect>? | |
var count = 0 | |
self.getRectsBeingDrawn(&rects, count: &count) | |
if let rects = rects { | |
for i in 0..<count { | |
let rect = rects[i] | |
let objectFrame: NSRect = <#object's frame#> | |
if NSIntersectsRect(objectFrame, rect) { | |
// drawing code for object | |
} | |
} | |
} | |
} | |
} | |
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CocoaViewsGuide/Optimizing/Optimizing.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyView: NSView { | |
override func draw(_ dirtyRect: NSRect) { | |
super.draw(dirtyRect) | |
guard let ctx = NSGraphicsContext.current?.cgContext else {return} | |
let objectFrame: NSRect = <#object's frame#> | |
if self.needsToDraw(objectFrame) { | |
// drawing code for object | |
} | |
} | |
} | |
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CocoaViewsGuide/Optimizing/Optimizing.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment