UIView Subview for Animation

2019-08-30 10:55发布

问题:

My goal is to animate my UI, and I am using UIView animations. 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. 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. 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.

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

回答1:

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



回答2:

This sounds like a case where you might consider the use of CABasicAnimations and potentially a CAAnimation group to perform your animations.

When you use the UIView beginAnimationsContext, the implicit properties you modify within the scope of the context (e.g., before you call [UIView commitAnimations]) will animate simultaneously.

Using CABasicAnimations is slightly more complicated, but accommodates the type of behavior you want. For example, you can add animations to specific views (rather, their layers).

Here's a simple tutorial.



回答3:

You can use multiple methods and have the animationDidStop method invoke each animation in turn, as the other poster suggested.

However, it's cleaner and easier to use the new block-based animation methods like 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.