多个UIViewanimation

我正试图一个接一个地执行多个UIViewanimation。 但是,我听说,一个接一个地执行多个UIViewanimation是不好的做法,而应该使用Core Animation。 我试过这个代码:

//First Animation [UIView beginAnimations:@"animation1" context:nil]; [UIView setAnimationDuration:2]; nwView.backgroundColor = [UIColor redColor]; nwView.frame = CGRectMake(CGRectGetMidX(screenSize), CGRectGetMinY(screenSize), width, height); nwView.transform = CGAffineTransformMakeRotation(45.0f); [UIView commitAnimations]; //Second Animation [UIView beginAnimations:@"second animation" context:nil]; [UIView setAnimationDuration:2]; nwView.transform = CGAffineTransformMakeScale(0.5, 0.33); nwView.backgroundColor = [UIColor purpleColor]; [UIView commitAnimations]; 

但它只做第二个animation。 我知道这个问题类似于UIView的两个animation并存 ,但是它有一个稍微不同的上下文。

我不认为使用UIView块连续做2个animation是有问题的。 只要确保你在第一个animation的完成块中开始你的第二个animation。

没有块(你的例子)它不工作,因为你将不得不设置委托给animation或setAnimationDidStopSelectorselect器。 你应该开始第二个animation。

但是,用块做animation也没有错(这是最好的方法)。

如果你正在寻找做多个animation,这是不是做到这一点。 您发布的代码几乎同时执行,这意味着animation将在大约同一时间执行。

相反,你应该做的是设置第一个animation的委托,然后做第一个animation。 然后,当为第一个animation调用animationDidStop方法时,应该执行第二个animation。 这确保他们是一个接一个。

假设你调用doMyAnimations来启动animation,这就是你要做的。

 -(void)doMyAnimations{ //First Animation [UIView beginAnimations:@"animation1" context:nil]; [UIView setAnimationDuration:2]; [UIView setAnimationDelegate:self]; nwView.backgroundColor = [UIColor redColor]; nwView.frame = CGRectMake(CGRectGetMidX(screenSize), CGRectGetMinY(screenSize), width, height); nwView.transform = CGAffineTransformMakeRotation(45.0f); [UIView commitAnimations]; } - (void)animationWillStart:(NSString *)animationID context:(void *)context{ } - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{ if([animationID isEqualToString:@"animation1"]){ //Second Animation [UIView beginAnimations:@"second animation" context:nil]; [UIView setAnimationDuration:2]; nwView.transform = CGAffineTransformMakeScale(0.5, 0.33); nwView.backgroundColor = [UIColor purpleColor]; [UIView commitAnimations]; } } 

请记住,nwView必须在整个课程中都可以访问。 如果不是,您可以将其设为实例variables,或者在animationDidStop方法中find另一种方法来访问它。

你可以使用这个块来获得一个非常干净的结果。

 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/