Graphics Programs Reference
In-Depth Information
CALayer *boxLayer;
}
@property (nonatomic, strong) UIColor *circleColor;
@end
The designated initializer for a CALayer is simply init . After you instantiate a layer,
you set its size, position (relative to its superlayer), and contents. In HypnosisView.m ,
change the initWithFrame: method to create a new layer and add it as a sublayer to
HypnosisView 's layer.
- (id)initWithFrame:(CGRect)r
{
self = [super initWithFrame:r];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
[self setCircleColor:[UIColor lightGrayColor]];
// Create the new layer object
boxLayer = [[CALayer alloc] init];
// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 85.0, 85.0)];
// Give it a location
[boxLayer setPosition:CGPointMake(160.0, 100.0)];
// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];
// Get a CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];
// Make it a sublayer of the view's layer
[[self layer] addSublayer:boxLayer];
}
return self;
}
Build and run the application. You will see a semi-transparent red block appear on the
view, as shown in Figure 22.4 .
Figure 22.4 Red layer
 
Search WWH ::




Custom Search