UIView iPhone SDK嵌套动画

时间:2009-10-03 18:06:47

标签: iphone uiview sdk animation

我想为我的视图设置一个嵌套动画。

我有一个动画停止选择器被调用就好了:

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

但是这个选择器里面的 我希望做更多动画,当完成另一个要调用的选择器时:

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

    [UIView commitAnimations];
}

问题是永远不会调用growAnimationDidStop2。这是为什么?

3 个答案:

答案 0 :(得分:3)

您也可以使用blocks执行此操作,Apple现在建议您这样做。请将以下内容视为伪代码:

[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) {}];
                                      }];
                   }];

将动画设置为“社交”也是一种很好的做法,例如:

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

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

在开始动画之前,然后在最后的完成块中执行:

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

答案 1 :(得分:1)

哎呀,自己回答。我必须在第一站方法

中开始一个全新的动画环境

答案 2 :(得分:1)

按顺序执行多个动画的最佳方法是使用块队列。

看看结果是多么干净:

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);

取自:http://xibxor.com/objective-c/uiview-animation-without-nested-hell/