Graphics Reference
In-Depth Information
To demonstrate some of the capabilities of matrix transforms we take a simple layer and
perform several different transforms on it. The first transform scales the layer from one
size to another. To start this example, build the layers shown in Listing 5-1.
LISTING 5-1
applicationDidFinishLaunching
- (
void
)applicationDidFinishLaunching:(
NSNotification
*)notification
{
NSView
*contentView = [[
self
window
]
contentView
];
CALayer *layer = [CALayer layer];
CGColorRef
color;
color =
CGColorCreateGenericRGB
(0.0f, 0.0f, 0.0f, 1.0f);
[layer
setBackgroundColor
:color];
[contentView
setLayer
:layer];
[contentView
setWantsLayer
:
YES
];
workLayer = [CALayer layer];
color =
CGColorCreateGenericRGB
(0.5f, 0.5f, 0.5f, 1.0f);
[workLayer setBackgroundColor:color];
[workLayer setCornerRadius:5.0f];
color =
CGColorCreateGenericRGB
(0.0f, 1.0f, 0.0f, 1.0f);
[workLayer setBorderColor:color];
[workLayer setBorderWidth:2.0f];
CGRect
workFrame = [layer
bounds
];
workFrame.
origin
.
x
= workFrame.
size
.
width
/ 4;
workFrame.
origin
.
y
= workFrame.
size
.
height
/ 4;
workFrame.
size
.
width
/= 2;
workFrame.
size
.
height
/= 2;
[workLayer setAnchorPoint:CGPointMake(0, 0)];
[
workLayer setFrame
:workFrame];
[layer addSublayer:workLayer];
}
In the
-applicationDidFinishLaunching:
method, we grab a reference to the
contentView
, set its layer and flag it as layer backed. By setting the layer, we are guaran-
teeing what type of layer the view uses for its backing.
When the
contentView
is set up properly, we next construct the layer that will be
manipulated. Its background color is set to gray, and the corners are rounded using
setCornerRadius:
. Next, the border color is set to green with a width of 2 pixels. Finally,