For the More Curious: Programmatically Generating Content

In this chapter, you set a layer’s contents with an image file. Now let’s look at setting a layer’s contents programmatically. There are two ways of drawing to a layer that use Core Graphics: subclassing and delegation.

In practice, subclassing is the last thing you want to do. The only reason to subclass CALayer to provide custom content is if you need to draw differently depending on some state of the layer. If this is the approach you wish to take, you must override the method drawInContext:.

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​L​a​y​e​r​S​u​b​c​l​a​s​s​ -​ ​(​v​o​i​d​)​d​r​a​w​I​n​C​o​n​t​e​x​t​:​(​C​G​C​o​n​t​e​x​t​R​e​f​)​c​t​x​ {​ ​ ​ ​ ​U​I​I​m​a​g​e​ ​*​l​a​y​e​r​I​m​a​g​e​ ​=​ ​n​i​l​;​ ...

Get iOS Programming: The Big Nerd Ranch Guide, Second Edition 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.