AnimationDidStop for group animation

I have a group animation, but I can’t detect when the animationDidStop hits. example of my code:

[group setDelegate:self];
[view.layer addAnimation:group forKey:@"groupAnimation"];

Does any of you know how I know when group animation is done?

+3
source share
3 answers

You also need to set the animationName property and verify that the delegate function is defined correctly:

CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 2.0f;
group.delegate = self;
[group setValue:@"groupAnimation" forKey:@"animationName"];
[group setAnimations:[NSArray arrayWithObjects:myAnimation, myOtherAnimation, nil]];
[view.layer addAnimation:group forKey:@"groupAnimation"];

. ,.

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
 if (finished)
 {
  NSString *animationName = [animation valueForKey:@"animationName"];
  if ([animationName isEqualToString:@"groupAnimation"])
  {
   // your groupAnimation has ended
  }
 }
}

Please note that in group animations, delegates installed in your component animations will be ignored.

+11
source

I use this category to configure completion as follows:

[group setCompletionBlock:^{

}];

First CAAnimationGroup + Blocks.h command:

#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>

typedef void (^TIFAnimationGroupCompletionBlock)();

@interface CAAnimationGroup (Blocks)

- (void)setCompletionBlock:(TIFAnimationGroupCompletionBlock)handler;

@end

And CAAnimationGroup + Blocks.m:

#import "CAAnimationGroup+Blocks.h"

static char CAAnimationGroupBlockKey;

@implementation CAAnimationGroup (Blocks)

- (void)setCompletionBlock:(TIFAnimationGroupCompletionBlock)handler {
    objc_setAssociatedObject(self, &CAAnimationGroupBlockKey, handler, OBJC_ASSOCIATION_COPY_NONATOMIC);

    self.delegate = self;
}

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
    if (finished)
    {
        TIFAnimationGroupCompletionBlock handler = (TIFAnimationGroupCompletionBlock)objc_getAssociatedObject(self, &CAAnimationGroupBlockKey);
        if (handler) {
            handler();
        }
    }
}

@end
+2
source

, . -, , :

typedef void (^animationCompletionBlock)(void);

, - :

#define kAnimationCompletionBlock @"animationCompletionBlock"

, , , , , , , :

animationCompletionBlock theBlock;
theBlock = ^void(void)
{    
  someButton.enabled = TRUE;
  NSLog(@"Animation complete");
  //whatever other code you want to do on completion
}

[myAnimation setValue: theBlock forKey: kAnimationCompletionBlock];

:

myAnimation.delegate = self

, , , , , :

/*
 This method looks for a value added to the animation that just completed 
 with the key kAnimationCompletionBlock.
 If it exists, it assumes it is a code block of type animationCompletionBlock, 
 and executes the code block.
 This allows you to add a custom block of completion code to any animation or 
 animation group, rather than Having a big complicated switch statement in your 
 animationDidStop:finished: method with global animation ompletion code.
 (Note that the system won't call the animationDidStop:finished method for 
 individual  animations in an animation group - it will only call the 
 completion method for the entire group. Thus, if you want to run code after 
 part of an animation group  completes, you have to set up a manual timer.)
*/

- (void)animationDidStop:(CAAnimation *)theAnimation 
  finished:(BOOL)flag
{
  animationCompletionBlock theBlock = 
    [theAnimation valueForKey: kAnimationCompletionBlock];
  if (theBlock)
    theBlock();
}

, , , , . , .

You can see this technique in the working program that I posted on Github:

Github Core Animation demo including completion block code

+1
source

All Articles