15

I am following the example at the bottom of the page to call an animationDidStop function.

http://www.informit.com/articles/article.aspx?p=1168314&seqNum=2

The author says:

I have an object that is designed specifically to be the delegate of animations and all it does is hold a reference to the target object, accept the animationDidStop: message and then release itself.

This suggests you shouldn't do:

[animation setDelegate:self];

I'm pretty new to app programming can someone outline how I might do this? Or send me a link where it is explained.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user157733
  • 569
  • 2
  • 12
  • 26

3 Answers3

38

Implement:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag

on your delegate object. You can also implement:

- (void)animationDidStart:(CAAnimation *)theAnimation

to receive a call when the animation starts.

For more info, see the Delegates section of: http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html

thefaj
  • 905
  • 9
  • 11
  • 8
    This is the correct answer. Why did someone vote this down, especially without comment? – thefaj Jul 29 '10 at 20:54
  • Then why does documentation say to use 'animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)' in UIView Class Reference section for the setAnimationDidStopSelector method? – CommaToast Dec 08 '15 at 20:09
4

Sometimes setting your layer's actual value to the toValue when the animation completes is required. When it's a more complex animation such as animating the colors of a CAGradientLayer, this is required.

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
  self.gradientLayer.colors = (NSArray *)((CABasicAnimation*)theAnimation).toValue;
}
james_womack
  • 10,028
  • 6
  • 55
  • 74
0

Just setting

[UIView setAnimationDelegate:self];

will not call any of the Animation delegate methods when the animation starts or ends.

This issue can be solved by one of the following workarounds.

1) In your implementation section add

@implementation MyViewWithAnimations <UIApplicationDelegate>


2) In your animation begin-commit-block add

[UIView setAnimationWillStartSelector:@selector(animationDidStart:)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];


3) Do what Apple suggests and use the block-based animation methods instead.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

lindinax
  • 282
  • 2
  • 6
  • Down voting without leaving a comment helps nobody. – lindinax Mar 18 '13 at 10:54
  • Even though I have not been the one downvoting, I am sure it happened because you are adding the “UIApplicationDelegate” to your class even though it has nothing to do with the animation’s delegate. – Git.Coach Aug 09 '15 at 21:55