Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer is not drawing its content while CABasicAnimation is used

I have an application with a custom CALayer subclass. In this layer subclass i have overwritten the - (void) drawInContext:(CGContextRef)ctx method. This works fine and all my custom content is drawn.

Now I wanted to add a custom property which gets animated when it's content is changed. And I need to redraw while the animation is running. I added the property like given in the answer to the following question: Animating a custom property of CALayer subclass

And I also added an CABasicAnimation instance for this property to my layer:

CABasicAnimation* theAnimation=[CABasicAnimation animationWithKeyPath:@"moveContentToLeft"];
theAnimation.duration=1.0;
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:10.0];
[area addAnimation:theAnimation forKey:@"moveContentToLeft"];

In my draw method I have a NSLog statement in which I output the value of my property I'm animating.

My problem is that as soon as the animation starts all the content of the layer subclass is cleared. The NSLog outputs the values of my property that gets interpolated (so the drawInContext method) is called. But somehow the things it draws are not visible during the animation. At the end of the animation the original content gets visible again.

At the moment the animated property is not yet used while drawing the layer so I would expect that the normal content would get drawn again and I get the output of the NSLog with the interpolated values.

My layer is a child of another layer (inside a UIView). I tried to redraw the super layer at the end of the drawing method (with calling _parent setNeedsDisplay [parent is a ivar to the parent view]). This did not help. And I also have the feeling that this is not a good idea to do so.

I hope somebody can help me with this problem.

Update

Thanks to the example project from Matt Long I could see that my problem lies inside the drawing method. I extended his project so it shows the same problem. I think it is simpler to show than the original code ;)

- (void)drawInContext:(CGContextRef)ctx
{
  NSLog(@"Getting called: Bob: %i", [self bob]);

  CGMutablePathRef path = CGPathCreateMutable();
  CGPathAddRect(path, NULL, CGRectMake(50 + [self bob], 50, 100, 100)); 
  CGContextAddPath(ctx, path);
  CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
  CGContextSetLineWidth(ctx, 1.0);
  CGContextStrokePath(ctx);
  CGPathRelease(path);

  [_test testInContext:ctx]; // here is the problem
}

The ivar _test is a custom class which contains some drawing code (at the moment it draws a green box). The problem now is that this green box will not be drawn while the animation runs. As soon as the animation is ended the green box is visible again. The extended example project: http://dl.dropbox.com/u/5426092/ArbitraryPropertyAnimationNew.zip

like image 261
Chris Avatar asked Nov 14 '22 08:11

Chris


1 Answers

It's hard to say what's going wrong without seeing more code. I have an example app that animates a custom property and the position of a layer at the same time. You can take a look at my source and see what's different.

like image 133
Matt Long Avatar answered Dec 19 '22 13:12

Matt Long