1
votes

I have an array of UIViews which I animate with the following code:

for (int i = 0; i<(int)viewArray.count; i++) {
    UIView *view = [viewArray objectAtIndex:i];
    CALayer *layer = view.layer;

    //animate the layer

}

My question is, is there any way to have a delay between every animation, so that, for example, it animates one UIView in the array and the next animation starts after 0.2 seconds or so? Any help would be greatly appreciated!

2

2 Answers

4
votes

You may want to try the following

float timeDelay = 0.2
float duration = YOUR_DURATION
for (int i = 0; i<(int)viewArray.count; i++) {
    UIView *view = [viewArray objectAtIndex:i];

    //animate the layer
    [UIView animateWithDuration:duration delay:(i+1)*timeDelay
            options: {Check UIView Class Reference on developer.apple.com}
            animations:^{
                   [view.layer fantastic animation here];
            } completion^(BOOL finised){
                   if(finished){
                     //Leave blank if nothing, good practice to implement debuging here or post  animation processes here (like removing a subview from a super)
                   }
            }];

}

Keep in mind that if u send your program the the background it may break your animations so make sure u recall this method in ur app Delegates:

- (void)applicationWillEnterForeground:(UIApplication *)application;

Hope it helps

0
votes

Use this guy:

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

use the second parameter. Inside your loop, call that function, passing an every-increasing number to that parameter.