views:

1797

answers:

5

Hi all. I'm wondering where the callbacks are (or if there are anything) for animations in a CALayer. Specifically, for implied animations like altering the frame, position, etc. In a UIView, you could do something like this:

[UIView beginAnimations:@"SlideOut" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animateOut:finished:context:)];
CGRect frame = self.frame;
frame.origin.y = 480;
self.frame = frame;
[UIView commitAnimations];

Specifically, the setAnimationDidStopSelector is what I want for an animation in a CALayer. Is there anything like that?

TIA.

A: 

I don't know of a way to do this other than using a separate timer, i.e. -performSelector:withObject:afterDelay:.

Noah Witherspoon
+5  A: 

I answered my own question. You have to add an animation using CABasicAnimation like so:

  CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"frame"];
  anim.fromValue = [NSValue valueWithCGRect:layer.frame];
  anim.toValue = [NSValue valueWithCGRect:frame];
  anim.delegate = self;
  [layer addAnimation:anim forKey:@"frame"];

And implement the delegate method animationDidStop:finished: and you should be good to go. Thank goodness this functionality exists! :D

Jeffrey Forbes
This answer is useful for the information about delegate, but it has one big flaw - you can't animate "frame" as it is a derived property. http://developer.apple.com/library/mac/#qa/qa2008/qa1620.html
Michal
A: 

Thanks for your post! I do have this question though- suppose I have 10 different animations and I want get separate callbacks, i.e., I don't want the animationDidStop:finished: function to fire after each animation. How do I register the animationDidStop:finished: to a particular CABasicAnimation object?

Any help would be appreciated!

A: 

You can set the name of a given animation when setting up the CAAnimation object. In animationDiStop:finished, just compare the name of theAnimation object provided to perform you specific functionality based on the animation.

JavaJoe
A: 

Hi Jeffrey,

You can't imagine how you save my life ! ;-) Thanks to take time to write your answer.

Alainms23

Alainms23