Graphics Reference
In-Depth Information
The stroke manipulations can also be applied when using the shape layer as a mask. In
fact, if you change the code from Listing 10-2 to set the layer mask instead of adding the
layer as we did in Listing 10-3, the view will now look like what you see in Figure 10-4.
See Listing 10-4 for the specific code changes.
LISTING 10-4
Using a CAShapeLayer with Stoke Manipulations as a Mask
- ( void )viewDidLoad
{
[ super viewDidLoad ];
UIImage *balloon = [ UIImage imageNamed : @”balloon.jpg” ];
[[[ self view ] layer ] setContents :( id )[balloon CGImage]];
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 ] setMask :shapeLayer];
}
As can be seen in Figure 10-4, the stroke manipulation is reflected in the masking of the
image and changes the clean edges into a jagged pattern that follows the shape of the
CAShapeLayer .
 
Search WWH ::




Custom Search