Graphics Reference
In-Depth Information
target's Build Phases tab (see Figure 1.4), and then import
<QuartzCore/QuartzCore.h>
in the view controller's
.m
file.
Figure 1.4
Adding the QuartzCore framework to the project
After doing that, we can directly reference
CALayer
and its properties and methods in our
code. In Listing 1.1, we create a new
CALayer
programmatically, set its
backgroundColor
property, and then add it as a sublayer to the
layerView
backing
layer. (The code assumes that we created the view using Interface Builder and that we have
already linked up the
layerView
outlet.) Figure 1.5 shows the result.
The
CALayer
backgroundColor
property is of type
CGColorRef
, not
UIColor
like the
UIView
class's
backgroundColor
, so we need to use the
CGColor
property
of our
UIColor
object when setting the color. You can create a
CGColor
directly using
Core Graphics methods if you prefer, but using
UIColor
saves you from having to
manually release the color when you no longer need it.
Listing 1.1
Adding a Blue Sublayer to the View
#import
"ViewController.h"
#import
<QuartzCore/QuartzCore.h>
@interface
ViewController
()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*layerView;