Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Animation for Pushing a UIViewController

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.


The code which you are looking for:

    [UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.80];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:
 UIViewAnimationTransitionFlipFromRight
                       forView:self.navigationController.view cache:NO];


[self.navigationController pushViewController:menu animated:YES];
[UIView commitAnimations];

What you could do is push the next view controller but don't animate it, like so:

[self.navigationController pushViewController:nextController animated:NO];

...and then, in the view controller that is getting pushed in, you could do a custom animation of it's view using CoreAnimation. This might be best done in the viewDidAppear:(BOOL)animated method.

Check out the Core Animation Guide on how to actually do the animation. Look particularly at the implicit animation.

EDIT: updated link


@zoul: That worked great! I just changed "self" to "self.navigationController" and "self.view" to "self.navigationController.view" Don't know if that was necessary, but it worked. And @crafterm, as for popping back, just make your own leftBarButtonItem by adding this code in viewDidLoad or ViewWillAppear:

//add your own left bar button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];

Then I just tweaked the push function and made this popWithTransition function that I called in my -backButtonTapped method.

- (void) popWithTransition: (UIViewAnimationTransition) transition
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    [UIView setAnimationBeginsFromCurrentState:YES];        
    [UIView setAnimationTransition:transition forView:self.navigationController.view cache:YES];
    [UIView commitAnimations];
[self.navigationController popViewControllerAnimated:NO];
}

Note that the popViewController call got shifted down to the end, after the animation. Don't know if that's kosher, but again, it worked.


What you want is the downloads for chapter 2 of iphone developers cookbook. Look at the affineRotate sample specifically, although any of the core animatin samples will help you.


Have a look at ADTransitionController, a drop in replacement for UINavigationController with custom transition animations (its API matches the API of UINavigationController) that we created at Applidium.

You can use different pre-defined animations for push and pop actions such as Swipe, Fade, Cube, Carrousel and so on. In your case, the animation you are requesting is the one called Zoom.