Reputation: 23550
I have 2 subviews that take the whole screen (excepted the status bar). Let's call this size "screen size".
I want to animated both :
the first to zoom from a little bit larger than the screen size to the screen size, from alpha 0 to alpha 1.
The second from screen size to a little bit smaller than screen size, from alpha 1 to alpha 0.
The second view is visible and on screen at start.
I wrote this :
- (void) switchViews
{
if (self.view2Controller == nil) {
self.view2Controller = [[View2Controller alloc] initWithNibName:@"View2XIB" bundle:nil];
self.view2Controller.view.hidden = YES;
[self.view addSubview:self.view2Controller.view];
}
CGRect bigFrame = CGRectInset(self.view.frame, -50, -50);
CGRect normalFrame = self.view.frame;
CGRect smallFrame = CGRectInset(self.view.frame, 50, 50);
self.view2Controller.view.frame = bigFrame;
self.view2Controller.view.alpha = 0.0;
[UIView beginAnimations:@"Anim1" context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
self.view2Controller.view.hidden = NO;
self.view2Controller.view.frame = normalFrame;
self.view2Controller.view.alpha = 1.0;
[UIView commitAnimations];
// ------------------------------
[UIView beginAnimations:@"Anim2" context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
self.view1Controller.view.frame = smallFrame;
self.view1Controller.view.alpha = 0.0;
[UIView commitAnimations];
}
Of course, I've tried first to put both animations into an unique one. That does not change anything, that's why I tried to separate them.
When launched, view1 goes immediatly to black, then the view2 starts animating as expected. But I can't achieve to run both animations at the same time.
How would I do that ?
Upvotes: 2
Views: 4840
Reputation: 1142
Try taking a look at block-based animations. It's the iOS 4.0+ recommended method. Take a look at the answer here: What are block-based animation methods in iPhone OS 4.0?
EDIT Try something like this
//You can do the same thing with a frame
CGPoint newCenter = CGPointMake(100, 100);
[UIView animateWithDuration:2.0
animations:^{
firstView.center = newCenter;
secondView.center = newCenter;
firstView.alpha = 0.2;
}
completion:^(BOOL finished){
NSLog(@"All done animating");
}];
Anything you put inside of animations: ^{ } will be the destination settings of your view. Above I showed you how to change the position as well as the alpha.
Upvotes: 2
Reputation: 6176
i cannot see anything wrong in your code. so i tried your code in my project and it work well: it does exactly what you wanted.
View1 move down-right and fade-out WHILE view2 come from top left and fade-in...
so i guess you have to look for an error somewhere else...
ciao,
luca
EDIT:
here's the code:
Upvotes: 0
Reputation: 38626
I think what you want to use is a CAAnimationGroup (Core Animation Group).
CAAnimationGroup allows multiple animations to be grouped and run concurrently. The grouped animations run in the time space specified by the CAAnimationGroup instance.
It has an animations
property which you set as the array of animations you want it to do with setAnimations:
.
Here is a usage example
Upvotes: 0