2

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 ?

Oliver
  • 23,072
  • 33
  • 138
  • 230

3 Answers3

2

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.

Community
  • 1
  • 1
Cameron
  • 1,142
  • 8
  • 32
  • I think those would be more appropriate for when you want to chain them, or have them occur in sequence (one directly after the other), but he wants them to occur at the same time. – Jorge Israel Peña Sep 29 '11 at 22:19
  • I get the chaining part, but could he not just have two similar block statements next to each other? So the blocks more or less fire at the same time? – Cameron Sep 29 '11 at 22:21
  • Why do you supose I'm coding for iOS 4 ? – Oliver Sep 29 '11 at 23:01
  • No need to antagonize here. You asked a question, and one of the proposed solutions is supported on about 94% of the devices out here. To be fair, if you want legacy support for 3.x, that's fine, but you didn't state that in your question. – Cameron Sep 30 '11 at 06:10
  • @Cameron : Please edit your answer to explain why this could solve the problem, so I can remove the downvote. – Oliver Sep 30 '11 at 06:46
0

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

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
  • Thank you. Could you see my edit ? And one bonus question : when defined the CAAnimationGroup, to wich view will I have to affect it ? The first view, the second one ? Or the parent of the 2 subviews that I want to animate ? – Oliver Sep 29 '11 at 23:20
  • the animation group will basically contain an array of both animations, each of which will affect the different views you want to animate. – Jorge Israel Peña Sep 29 '11 at 23:44
0

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:

test-switch.zip

meronix
  • 6,175
  • 1
  • 23
  • 36
  • Uhh... Nooo, you're kidding ? I'll try to isolate the code as you did tonight. :-) – Oliver Sep 30 '11 at 08:45
  • ah ah... sorry, but no... your searching-field is a bit larger than the code you sent... try my isolated code (see new edited answer) – meronix Sep 30 '11 at 11:13