我想为我的视图设置一个嵌套动画。
我有一个动画停止选择器被调用就好了:
[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
。这是为什么?
答案 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/