Graphics Reference
In-Depth Information
LISTING 10-2
Continued
CGMutablePathRef path = CGPathCreateMutable ();
CGPathMoveToPoint (path, NULL , 0, 100);
CGPathAddLineToPoint (path, NULL , 200, 0);
CGPathAddLineToPoint (path, NULL , 200, 200);
CGPathAddLineToPoint (path, NULL , 0, 100);
shapeLayer = [[ CAShapeLayer alloc ] init ];
[shapeLayer setBounds : CGRectMake (0, 0, 200, 200)];
[shapeLayer setFillColor :[[ UIColor purpleColor ] CGColor ]];
[shapeLayer setPosition : CGPointMake (200, 200)];
[shapeLayer setPath :path];
[shapeLayer setStrokeColor :[[ UIColor redColor ] CGColor ]];
[shapeLayer setLineWidth :10.0f];
[shapeLayer setLineJoin : kCALineJoinRound ];
[shapeLayer setLineDashPattern :
[ NSArray arrayWithObjects :[ NSNumber numberWithInt :50],
[ NSNumber numberWithInt :2],
nil ]];
[[[ self view ] layer ] addSublayer :shapeLayer];
}
The first thing we do is set the stroke color to red and set its width to 10 pixels. To round
the corners of the path, kCALineJoinRound is used to join the lines and round corners.
The radius of the corners is based on the thickness of the lines being joined and the angle
of the intersection of the lines and, therefore, cannot be adjusted directly. The last thing
we give the line is a dashed pattern. In Listing 10-2, the layer is stroked with 50 units of
red-colored line to create the dash, followed by 2 units of unpainted space, which gives
you the gap between the dashes.
What Is a “Unit”?
The term unit is used here instead of pixels because of resolution independence . Apple intro-
duced to developers the concept of resolution independence at WWDC 2006 as part of its
migration strategy for Mac OS X Leopard and beyond. The benefit of resolution independence
guarantees that regardless of which device the application is used on—be it an iPhone, iPod
touch, a MacBook Pro screen, or a 30-inch Apple Cinema HD Display—will look the same
from one screen to the next, regardless of what the user-space-unit is. As you can imagine,
if we were to use pixel-perfect settings instead of units, the layer would look much, much
different from one screen (and one resolution) to the next.
 
Search WWH ::




Custom Search