简体   繁体   English

动画的UIView子视图

[英]UIView Subview for Animation

My goal is to animate my UI, and I am using UIView animations. 我的目标是为UI设置动画,并且正在使用UIView动画。 The problem is I need more than one animation going on at a time, but apparently I can only perform one UIView animation at a time. 问题是我一次需要一个以上的动画,但是显然我一次只能执行一个UIView动画。 I have asked questions on this topic previously ( Multiple UIView Animations ), and all the responders say I have to set a delegate to the animation with a method like animationDidStop:performSelector: but I was wondering if I could instead add subviews to the main view and perform animations simultaneously on each subview. 我以前曾问过这个主题的问题( 多个UIView动画 ),所有响应者都说我必须使用animationDidStop:performSelector:类的方法设置动画的委托animationDidStop:performSelector:但是我想知道是否可以改为将子视图添加到主视图中并在每个子视图上同时执行动画。 Also, I am unable to perform back to back animations, and I was thinking maybe I could perform an animation on view1 and then on view2 without delegating. 另外,我无法执行背对背动画,并且我想也许可以在不委托的情况下在view1然后在view2上执行动画。

For example: 例如:

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

I dont see the problem here, if what you need is to animate two diffent views at once do the following: 如果您需要立即为两个不同的视图制作动画,则在这里看不到问题,请执行以下操作:

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

Also add the following 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.....
}

The animations should occur sequentially 动画应顺序发生

This sounds like a case where you might consider the use of CABasicAnimations and potentially a CAAnimation group to perform your animations. 这听起来像是您可能考虑使用CABasicAnimations以及可能使用CAAnimation组来执行动画的情况。

When you use the UIView beginAnimationsContext, the implicit properties you modify within the scope of the context (eg, before you call [UIView commitAnimations]) will animate simultaneously. 使用UIView beginAnimationsContext时,在上下文范围内(例如,在调用[UIView commitAnimations]之前)修改的隐式属性将同时进行动画处理。

Using CABasicAnimations is slightly more complicated, but accommodates the type of behavior you want. 使用CABasicAnimations稍微复杂一些,但是可以适应您想要的行为类型。 For example, you can add animations to specific views (rather, their layers). 例如,您可以将动画添加到特定视图(而不是它们的图层)。

Here's a simple tutorial . 这是一个简单的教程

You can use multiple methods and have the animationDidStop method invoke each animation in turn, as the other poster suggested. 您可以使用多种方法,并让animationDidStop方法依次调用每个动画,如其他海报所建议的那样。

However, it's cleaner and easier to use the new block-based animation methods like animateWithDuration:animations:completion. 但是,使用新的基于块的动画方法(例如animateWithDuration:animations:completion)更加简洁方便。

That method lets you pass in a completion block that gets executed once the animation finishes. 该方法允许您传递一个完成块,动画完成后立即执行该完成块。 That code can then invoke the next animation in your sequence. 然后,该代码可以调用序列中的下一个动画。

The animations block can animate multiple views at the same time if you want. 如果需要,动画块可以同时为多个视图设置动画。 (so can beginAnimations/commitAnimations, for that matter. You just apply changes to multiple views between the beginAnimations and commitAnimations calls. (对此,beginAnimations / commitAnimations也可以。您只需将更改应用于beginAnimations和commitAnimations调用之间的多个视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM