Graphics Reference
In-Depth Information
LISTING 4-9
Continued
CFRelease (white);
CFRelease (black);
return layer;
}
NOTE
Use an Image as Layer Content
If you prefer, you can instead use an image for the close box layer contents. Just load the
image in an NSImage for OS X or a UIImage for the iPhone and get a CGImageRef object.
Then set the layer's contents to that CGImageRef . Preferring an image over explicitly drawing
is mostly a matter of preference; however, for our purposes, Listings 4-9 and 4-10 demon-
strate how to achieve the desired look by drawing explicitly.
LISTING 4-10
Draw an X In the Close Box Layer
- ( void )drawLayer:( CALayer *)layer
inContext:( CGContextRef )context
{
// Make sure the call is applied to the close
// box layer
if ( layer == closeLayer )
{
// Create the path ref
CGMutablePathRef path = CGPathCreateMutable ();
// Set the first point and add a line
CGPathMoveToPoint (path, NULL ,10.0f,10.f);
CGPathAddLineToPoint (path, NULL , 20.0, 20.0);
// Set the second point and add a line
CGPathMoveToPoint (path, NULL ,10.0f,20.f);
CGPathAddLineToPoint (path, NULL , 20.0, 10.0);
// Set the stroke color to white
CGColorRef white =
CGColorCreateGenericRGB (1.0, 1.0, 1.0, 1.0);
CGContextSetStrokeColorWithColor (context, white);
CGColorRelease (white);
 
Search WWH ::




Custom Search