For the More Curious: Dirty Rects

As mentioned previously, the NSRect that is passed as an argument to the view is the region that is “dirty” and needs redrawing. It may be less than the entire view. If you are doing very time-consuming drawing, redrawing only the dirty rectangle may speed up your application considerably.

Note that setting needsDisplay to true will trigger the entire visible region of the view to be redrawn. If you wanted to be more precise about which part of the view needs redrawing, you would use setNeedsDisplayInRect(_:) instead:

 ​ ​l​e​t​ ​d​i​r​t​y​R​e​c​t​ ​=​ ​N​S​R​e​c​t​(​x​:​ ​0​,​ ​y​:​ ​0​,​ ​w​i​d​t​h​:​ ​5​0​,​ ​h​e​i​g​h​t​:​ ​5​0​)​ ​ ​m​y​V​i​e​w​.​s​e​t​N​e​e​d​s​D​i​s​p​l​a​y​I​n​R​e​c​t​(​d​i​r​t​y​R​e​c​t​)​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.