Skip to content

Instantly share code, notes, and snippets.

Forked from andrewsardone/gist:3751168
Created September 3, 2023 09:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
How does UIKit get pixels onto the screen?

UIView Drawing Model

-[UIView setNeedsDisplay]

You have some kind of custom UIView which implements some awesome drawing in -[UIView drawRect:]. In order to have UIKit draw it for you, you send the -[UIView setNeedsDisplay] to the view object to queue it up for drawing.

-[CALayer setNeedsDisplay]

     *------------*           *------------*
     |            |           |            |
     |   UIView   |  ------>  |   CALayer  |
     |            |           |            |
     *------------*           *------------*
-[UIView setNeedsDisplay] -[CALayer setNeedsDisplay]

The UIView object has another, special object backing it, a CALayer. When its convenient for the rendering system, it'll ask the view's CALayer to display by sending it the -[CALayer display] message.

Now, the CALayer creates a region of memory called a "Backing Store," a bitmap that stores the result of all the rendering operations.

     *------------*           *------------*          ..............
     |            |           |            |          .            .
     |   UIView   |  ------>  |   CALayer  |  ----->  .   Backing  .
     |            |           |            |          .    Store   .
     *------------*           *------------*          ..............
                            -[CALayer display]

This backing store image can be displayed on screen repeatedly until the view needs some different drawing (e.g., for a highlighted state). At that point, the process would start over by sending the view the -[UIView setNeedsDisplay] message.

For a better walk through, check out the in-depth aside on UIView drawing in the WWDC 2012 presentation, Building Concurrent User Interfaces on iOS by Andy Matuschak (video requires an ADC login).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment