UIViewanimation子视图

我的目标是animation我的用户界面,我正在使用UIViewanimation。 问题是我需要一次一个以上的animation,但显然我一次只能执行一个UIViewanimation。 以前我曾经问过关于这个主题的问题( 多个UIViewanimation ),并且所有的响应者都说我必须用animationDidStop这样的方法来设置一个委托给animationDidStop:performSelector:但是我想知道是否我可以添加子视图到主视图并在每个子视图上同时执行animation。 此外,我无法执行背靠背animation,我想也许我可以执行一个animationview1,然后view2没有委托。

例如:

 //Header @property (nonatomic, strong) UIView *view1; @property (nonatomic, retain) UIView *view2; //Implementation @synthesize view1; @synthesize view2; //ViewDidLoad view1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,480,640)]; view2 = [[UIView alloc]initWithFrame:CGRectMake(0,0,480,640)]; view1.backgroundColor = [UIColor clearColor]; view2.backgroundColor = [UIColor clearColor]; [performAnimationsOn View1]; //I don't know the code I would put here because the code //I usually use is [UIView beginAnimation:@"animation" //context:nil]; but that is a class method and I am not //sure how to perform an animation on a specific subview. [performAnimationsOn View2]; 

我没有看到这里的问题,如果你需要的是一次animation两个不同的意见做以下几点:

 //First add these subviews [self.view addSubview:view1]; [self.view addSubview:view2]; [UIView beginAnimations:@"Animation1" context:nil]; [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:1]; //Do something with view 1 move, change alpha, change transformation etc.. [UIView commitAnimations]; 

另外添加以下function

 - (void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { if ([animationID isEqualToString:@"Animation1"]) { [UIView beginAnimations:@"Animation2" context:nil]; [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:1]; //Do something with view 2 move, change alpha, change transformation etc.. [UIView commitAnimations]; } else if ([animationID isEqualToString:@"Animation2"]) { [UIView beginAnimations:@"Animation3" context:nil]; [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:1]; //Do something with view 3 move, change alpha, change transformation etc.. [UIView commitAnimations]; } //And so on..... } 

animation应该按顺序进行

这听起来像是你可能会考虑使用CABasicAnimations和潜在的CAAnimation小组来执行你的animation。

当你使用UIView的beginAnimationsContext,你在上下文范围内修改的隐式属性(例如,在你调用[UIView commitAnimations]之前)将同时animation。

使用CABasicAnimations稍微复杂一点,但是可以容纳你想要的行为types。 例如,你可以添加animation到特定的视图(而不是他们的图层)。

这是一个简单的教程 。

您可以使用多个方法,并使animationDidStop方法依次调用每个animation,如另一张海报所示。

但是,使用animateWithDuration:animations:completion等新的基于块的animation方法更简单,更简单。

该方法允许您传入一个完成块,一旦animation完成,就会执行完成块。 该代码然后可以调用序列中的下一个animation。

如果需要,animation块可以同时animation多个视图。 (所以可以开始animation/提交animation,你只需要对beginAnimations和commitAnimations调用之间的多个视图进行更改。