[英]UIView Subview for Animation
我的目标是为UI设置动画,并且正在使用UIView动画。 问题是我一次需要一个以上的动画,但是显然我一次只能执行一个UIView动画。 我以前曾问过这个主题的问题( 多个UIView动画 ),所有响应者都说我必须使用animationDidStop:performSelector:
类的方法设置动画的委托animationDidStop:performSelector:
但是我想知道是否可以改为将子视图添加到主视图中并在每个子视图上同时执行动画。 另外,我无法执行背对背动画,并且我想也许可以在不委托的情况下在view1然后在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];
如果您需要立即为两个不同的视图制作动画,则在这里看不到问题,请执行以下操作:
//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.....
}
动画应顺序发生
这听起来像是您可能考虑使用CABasicAnimations以及可能使用CAAnimation组来执行动画的情况。
使用UIView beginAnimationsContext时,在上下文范围内(例如,在调用[UIView commitAnimations]之前)修改的隐式属性将同时进行动画处理。
使用CABasicAnimations稍微复杂一些,但是可以适应您想要的行为类型。 例如,您可以将动画添加到特定视图(而不是它们的图层)。
这是一个简单的教程 。
您可以使用多种方法,并让animationDidStop方法依次调用每个动画,如其他海报所建议的那样。
但是,使用新的基于块的动画方法(例如animateWithDuration:animations:completion)更加简洁方便。
该方法允许您传递一个完成块,动画完成后立即执行该完成块。 然后,该代码可以调用序列中的下一个动画。
如果需要,动画块可以同时为多个视图设置动画。 (对此,beginAnimations / commitAnimations也可以。您只需将更改应用于beginAnimations和commitAnimations调用之间的多个视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.