Custom Animation for Pushing a UIViewController

Erik picture Erik · Sep 10, 2009 · Viewed 70.5k times · Source

I want to show a custom animation when pushing a view controller: I would like to achieve something like an "expand" animation, that means the new view expands from a given rectangle, lets say [100,100 220,380] during the animation to full screen.

Any suggestions where to start, respectively any documents, tutorials, links? :)


Alright. I could make the expand animation with the following code:

if ([coming.view superview] == nil)   
    [self.view addSubview:coming.view];
    coming.view.frame = CGRectMake(160,160,0,0);
    [UIView beginAnimations:@"frame" context:nil];
    [UIView setAnimationDuration:4];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [coming viewWillAppear:YES];
    [going viewWillAppear:YES];
    coming.view.frame = CGRectMake(0, 0, 320, 480);
    [going viewDidDisappear:YES];
    [coming viewDidAppear:YES];
    [UIView commitAnimations];

My View is properly displayed, but unfortunately the navigation bar is not updated. Is there a way to do that manually?


In the sample code, a function is called all 0.03 seconds that updates the transformation of the view. Unfortunately, when pushing a UIViewController, I am not able to resize the frame of the view ... am I ?

Answer

zoul picture zoul · Mar 15, 2010

I use the following function (added to UINavigationController) to customize the push animation:

- (void) pushController: (UIViewController*) controller
         withTransition: (UIViewAnimationTransition) transition
{
    [UIView beginAnimations:nil context:NULL];
    [self pushViewController:controller animated:NO];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationBeginsFromCurrentState:YES];        
    [UIView setAnimationTransition:transition forView:self.view cache:YES];
    [UIView commitAnimations];
}

I guess you could adapt this code to do whatever animation you want.