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];
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.
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.
I dont see the problem here, if what you need is to animate two diffent views at once do the following:
Also add the following function
The animations should occur sequentially