CALayer won't display
- by Paul from Boston
I'm trying to learn how to use CALayers for a project and am having trouble getting sublayers to display. I created a vanilla View-based iPhone app in XCode for these tests. The only real code is in the ViewController which sets up the layers and their delegates. There is a delegate, DelegateMainView, for the viewController's view layer and a second different one, DelegateStripeLayer, for an additional layer. The ViewController code is all in awakeFromNib,
- (void)awakeFromNib {
DelegateMainView *oknDelegate = [[DelegateMainView alloc] init];
self.view.layer.delegate = oknDelegate;
CALayer *newLayer = [CALayer layer];
DelegateStripeLayer *sldDelegate = [[DelegateStripeLayer alloc] init];
newLayer.delegate = sldDelegate;
[self.view.layer addSublayer:newLayer];
[newLayer setNeedsDisplay];
[self.view.layer setNeedsDisplay];
}
The two different delegates are simply wrappers for the CALayer delegate method, drawLayer:inContext:, i.e.,
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
CGRect bounds = CGContextGetClipBoundingBox(context);
... do some stuff here ...
CGContextStrokePath(context);
}
each a bit different. The layer, view.layer, is drawn properly but newLayer is never drawn. If I put breakpoints in the two delegates, the program stops in DelegateMainView but never reaches DelegateStripeLayer. What am I missing here? Thanks.