Graphics Reference
In-Depth Information
Working with Layers
Let's start by creating a simple project that will allow us to manipulate the properties of a
layer. In Xcode, create a new iOS project using the Single View Application template.
Create a small view (around 200×200 points) in the middle of the screen. You can do this
either programmatically or using Interface Builder (whichever you are more comfortable
with). Just make sure that you include a property in your view controller so that you can
access the small view directly. We'll call it layerView .
If you run the project, you should see a white square in the middle of a light gray
background (see Figure 1.3). If you don't see that, you may need to tweak the background
colors of the window/view.
Figure 1.3 A white UIView on a gray background
That's not very exciting, so let's add a splash of color. We'll place a small blue square
inside the white one.
We could achieve this effect by simply using another UIView and adding it as a subview
to the one we've already created (either in code or with Interface Builder), but that
wouldn't really teach us anything about layers.
Instead, let's create a CALayer and add it as a sublayer to the backing layer of our view.
Although the layer property is exposed in the UIView class interface, the standard
Xcode project templates for iOS apps do not include the Core Animation headers, so we
cannot call any methods or access any properties of the layer until we add the appropriate
framework to the project. To do that, first add the QuartzCore framework in the application
 
Search WWH ::




Custom Search