Difference between UIView's drawRect and CALayer?

Discussion in 'iOS Programming' started by Apple souce, Nov 2, 2012.

  1. Apple souce macrumors member

    Joined:
    Mar 15, 2012
    #1
    Probably a noob question, but I am having trouble figuring out why does CALayer exist when every UIView can display itself via the drawRect method?

    Also, does the end of the drawRect method make a CALayer??

    Reading the big nerd ranch ios book and stuck on the beginning of the core animation layer chapter.
     
  2. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #2
    Think of UIView as a wrapper around CALayer. Each UIView has one CALayer backing it. But a CALayer isn't necessarily associated with a UIView.
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    Every UIView has a CALayer. It's what a drawRect draws into.

    UIViews, which encapsulate a bunch of stuff, including a built-in CALayer and event responder (etc.), are usually easier to use than all the sub-parts individually.

    But using CALayers directly has the advantage that you can draw, swap, change, and update them in background threads (not so with most UI objects), and thus not have to wait for a drawRect callback, nor slow down the responsiveness of the app's UI main run loop with any lengthly draw.
     

Share This Page