Graphics Reference
In-Depth Information
iPhone/iPod touch. In the Icon Dance application, the close box doesn't do anything, but
it enables you to see the effect.
For this example, we will do the following:
.
Set the border width for the close box layer to 3 pixels.
.
Set the frame to 30
×
30 pixels.
.
Set the corner radius to 15 (half the frame size) to make it look like a circle.
.
Set the background color to black.
.
Set the drop shadow color to black.
.
Set the shadow radius to 5 pixels.
.
Set the shadow opacity to 1.0 (opaque).
The code in Listing 4-9 implements each of these items. Notice that we set the delegate
for the layer to
self
. This enables us to call
- (void)drawLayer:(CALayer *)theLayer
inContext:(CGContextRef) theContext
(see the beginning of Listing 4-10), which
enables us to add the code that draws the X in the middle of the close box layer. The code
used to draw the X (shown in Listing 4-10) uses a Core Graphics path that sets a point
and draws the first leg, and then sets another point and draws the second leg.
LISTING 4-9
Define the Close Box Layer
-(
CALayer
*)closeBoxLayer;
{
CGColorRef
white =
CGColorCreateGenericRGB
(1.0, 1.0, 1.0, 1.0);
CGColorRef
black =
CGColorCreateGenericRGB
(0.0, 0.0, 0.0, 1.0);
CALayer
*layer = [
CALayer layer
];
[layer
setFrame
:
CGRectMake
(0.0,
kCompositeIconHeight - 30.0,
30.0, 30.0)];
[layer
setBackgroundColor
:black];
[layer
setShadowColor
:black];
[layer setShadowOpacity:1.0];
[layer setShadowRadius:5.0];
[layer
setBorderColor
:white];
[layer
setBorderWidth
:3];
[layer
setCornerRadius
:15];
[layer
setDelegate
:
self
];
// Release the color refs