开发者

UIView iPhone SDK nested animations

I would like to have a nested animation for my view.

I have one animation stop selector which gets called just fine:

[UIView setAnimationDidStopSelector:@selector(growAnimationDidStop1:finished:context:)];

However inside of this selector I want to do more animation and when done yet another selector to be called:

- (void)growAnimationDidStop1:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{
...
    [UIView setAnimationDidStopSelector:@selector(growAnimationDidStop2:finished:context:)];
... 

    [UIView commitAnimations];
}

The problem is tha开发者_JAVA百科t growAnimationDidStop2 is never called. Why is this?


You can also do this with blocks, and Apple now recommends this. Consider the following as pseudo-code:

[UIView animateWithDuration:0.3
                        delay:0.0
                      options:UIViewAnimationCurveEaseInOut
                   animations:^{
                     [self doAnimationOne];
                   } completion:^(BOOL finished){
                     [UIView animateWithDuration:0.4
                                           delay:0.0
                                         options:UIViewAnimationCurveEaseInOut
                                      animations:^{
                                        [self doAnimationNine];
                                      }
                                      completion:^(BOOL finished) {
                                        [UIView animateWithDuration:0.3
                                                              delay:0.0
                                                            options:UIViewAnimationCurveEaseInOut
                                                         animations:^{
                                                           [self doAnimationFive];
                                                         } 
                                                         completion:^(BOOL finished) {}];
                                      }];
                   }];

It's also good practice for your animations to be "social", for instance do:

BOOL userInteractionEnabled = [self isUserInteractionEnabled];
[self setUserInteractionEnabled:NO];

BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:YES];

before starting your animation, and then in the final completion block do:

[UIView setAnimationsEnabled:animationsEnabled];
[self setUserInteractionEnabled:userInteractionEnabled];


Oops, answered it myself. I had to start a brand new animation context in the first stop method


The best way to do multiple animations in sequence is with a queue of blocks.

See how clean the result is:

NSMutableArray* animationBlocks = [NSMutableArray new];

typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
    animationBlock block = (animationBlock)[animationBlocks firstObject];
    if (block){
        [animationBlocks removeObjectAtIndex:0];
        return block;
    }else{
         return ^(BOOL finished){};
    }
};

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    NSLog(@"Multi-step Animation Complete!");
}];

// execute the first block in the queue
getNextAnimation()(YES);

Taken from: http://xibxor.com/objective-c/uiview-animation-without-nested-hell/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜