CALayer when is backgroundColor, shadow, cornerRadius, etc drawn?
If you create a CALayer subclass and override drawInContext, display, or
any of the drawing routines for that matter, then animate a basic
property, you'll find the drawing routines are never called. So, where do
they occur?
Longer Version
Create a new project, a single-window application for iPhone
Add QuartzCore
Make the UIViewController look like so:
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
HACLayerAnimator *layer = [HACLayerAnimator layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(100, 100);
layer.backgroundColor = [UIColor blackColor].CGColor;
[self.view.layer addSublayer:layer];
[self performSelector:@selector(test:) withObject:layer afterDelay:1];
}
- (void)test:(HACLayerAnimator *)layer
{
CABasicAnimation *anim = [CABasicAnimation
animationWithKeyPath:@"backgroundColor"];
[anim setDuration:5];
[anim setFromValue:(id)layer.backgroundColor];
[anim setToValue:(id)[UIColor redColor].CGColor];
layer.backgroundColor = [UIColor redColor].CGColor;
[layer addAnimation:anim forKey:@"backgroundColor"];
}
4 . Add a CALayer subclass HACLayerAnimator with this
as the body
- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
{
NSLog(@"drawing cell %@", self);
}
- (void)drawInContext:(CGContextRef)ctx
{
NSLog(@"drawing cell %@", self);
}
- (void)renderInContext:(CGContextRef)ctx
{
NSLog(@"drawing cell %@", self);
}
- (void)display
{
NSLog(@"drawing cell %@", self);
}
You will find that, although the layer displays fine and animates
smoothly, none of the drawing methods are ever called.
The documentation states this:
- (void)drawInContext:(CGContextRef)ctx
The default implementation of this method does not doing any drawing itself.
So, when does CALayer draw its 'built-in' properties?
No comments:
Post a Comment