2012-06-03 92 views
0

我的目标是我的动画UI,和我使用UIView的动画。问题是我需要同时进行多个动画,但显然我一次只能执行一个UIView动画。我之前在这个主题上提出过问题(Multiple UIView Animations),所有响应者都说我必须用animationDidStop:performSelector:这样的方法设置一个委托给动画,但我想知道是否可以将子视图添加到主视图并同时执行动画每个子视图。另外,我无法执行背靠背动画,我想也许我可以在view1上执行动画,然后在view上执行动画而不进行委托。的UIView子视图的动画

例如:

//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]; 

回答

1

我没有看到这里的问题,如果你需要的是动画2点diffent意见同时做到以下几点:

//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]; 

而且添加的功能

- (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..... 
} 

动画应该按顺序发生

+0

啊..确定虐待更新我的答案 –

+0

请检查更新的答案:) –

+0

是在执行。 M档 –

0

这听起来像是在那里你可以考虑使用CABasicAnimations的和潜在的一个CAAnimation组来执行你的动画的情况。

当您使用UIView beginAnimationsContext时,您在上下文范围内修改的隐式属性(例如,在调用[UIView commitAnimations]之前)将同时进行动画处理。

使用CABasicAnimations稍微复杂一点,但容纳您想要的行为类型。例如,您可以将动画添加到特定的视图(而不是其图层)。

这里有一个简单的tutorial

+0

感谢。这也有帮助。尽管如此我仍然没有投票。对不起 – pasawaya

+0

只有组中的所有动画在同一图层上操作,动画组才能正常工作。我四处奔走,最后得到了苹果工程师的确认。 CABasicAnimation对象功能更强大,但使用起来要复杂得多。 –

0

您可以使用多种方法,并让animationDidStop方法依次调用每个动画,如其他贴图所示。

但是,使用像animateWithDuration:animations:completion这样的基于块的新动画方法更简单,更简单。

这种方法可以让你在得到一次在动画完成执行完成块传递。该代码然后可以调用序列中的下一个动画。

如果需要,动画块可以同时动画多个视图。 (所以可以beginAnimations/commitAnimations,对于这个问题,你只要更改应用到beginAnimations和commitAnimations电话之间的多个视图。