views:

93

answers:

1

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: http://stackoverflow.com/questions/2395382/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

A: 

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.

Matt Long
Thanks for your example code. I extended it so it behave the same as my code (see my question).
Chris
The real question at this point is what are you trying to do? I've looked at your code changes, but I can't figure for the life of me what you want it to do. If you are just trying to animate a couple of rectangles, there is absolutely no reason to draw it yourself. You should just be adding your two rectangles as layers and animating their positions like my example code showed. Your custom property is completely unnecessary. So what are you trying to do again?
Matt Long