Avba
Avba

Reputation: 15266

How to know when UIView animations have all completed

A custom view I created is being animated by its container - in my view I update portions of the subviews with other animations (for instance a UICOllectionView)

I see that this is throwing errors

*** Assertion failure in -[UICollectionView _endItemAnimations

Digging around I see that my view has animations attached to it:

<GeneratorView: 0x15b45950; frame = (0 0; 320 199); animations = { position=<CABasicAnimation: 0x145540a0>; }; layer = <CALayer: 0x15b49410>>

So now before performing animation operations I check:

NSArray *animationKeys = [self.layer animationKeys];
    for (NSString *key in animationKeys) {
        CAAnimation *animation = [self.layer animationForKey:key];

    }

and I see that animation objects are returned:

at this point I would like to "wait" until all animations have completed before updating self.

I see that I can add myself as the CAAnimation delegate.

But this is a bit messy and hard to track.

Is there an easier way using a UIView method - much higher level?

Upvotes: 3

Views: 2028

Answers (2)

Greg
Greg

Reputation: 25459

You can use UIView method to do the animation in the container:

[UIView animateWithDuration: animations: completion:];
[UIView animateWithDuration: delay: options: animations: completion:];

You should add properties to your custom view:

@property BOOL isAnimating;

In the container run the animation block and change view isAnimating property. This method accept block which will be fired when the animation complete. Example:

[UIView animateWithDuration:1.0 animations:^{
        //Your animation block
        view1.isAnimating = YES;
        view1.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
        // etc.
    }completion:^(BOOL finished){
        view1.isAnimating = NO;
        NSLog(@"Animation completed.");
    }];

Now it your view you can see if the view is animating:

if (self.isAnimating)
    NSLog(@"view is animating");

Upvotes: 3

Nicolas Bonnet
Nicolas Bonnet

Reputation: 1274

Ithink you can you that :

@interface UIView(UIViewAnimationWithBlocks)

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

Hope that will help

Upvotes: 0

Related Questions