O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​)​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required