Search code examples
cocoa-touchcore-animationcore-graphicscalayer

Drawing with Core Graphics in a CALayer


I'm trying to get my head around Core Graphics and Core Animation. I understand the following:

  • Using UIView, I can draw with Core Graphics by overriding drawRect.
  • I can create multiple CALayer's and set their properties and have them added

My questions are: - If I create a CALayer from scratch (not using something like CALayer *myLayer = myUIView.layer), what is the approach to draw in that CALayer? - What's the CALayer's equivalent of drawRect for UIView?

I hope that makes sense.


Solution

  • To do custom drawing you have two options:

    1. Subclass CALayer and implement -drawInContext:.
    2. Make your own class that serves as your layer's delegate to perform the Quartz routines. It should implement -drawLayer:inContext:.

    You should add your custom layer as a sublayer to your UIView's layer. You should take a spin through CALayer's doc pages for some rules about how to interact with the view's layer.