Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom animation between UIViewControllers using transitionFromViewController:toViewController:duration

I am trying to use UIViewController's transitionFromViewController:toViewController:duration method but with a custom animation.

I have the following two view controllers added as children to a custom container UIViewController:

  1. firstController - This is an instance of UITabBarController
  2. secondController - This is a subclass of UIViewController

The following code works as expected:

[self transitionFromViewController:firstController
                  toViewController:secondController 
                          duration:2                                         
                           options:UIViewAnimationOptionTransitionFlipFromLeft  
                        animations:^(void){} 
                        completion:^(BOOL finished){}];

However I would like to create a custom animation where the where firstController slides to the left and is replaced by secondController sliding in from the right similar to how UINavigationControllers push and pop methods work. After changing the options to UIViewAnimationOptionTransitionNone I have tried to implement custom animations in the animations block but have had absolutely no success. firstController is immediately swapped for secondController without and animations.

I would really appreciate any help.

Thank you

like image 209
Simple99 Avatar asked Aug 17 '12 15:08

Simple99


1 Answers

This is actually really easy. For some reason I assumed that the secondController's view would be be under/ behind that of firstController's. I had only tried to animate the firstController's view. This of course is wrong. As soon as transitionFromViewController:toViewController:duration is called secondController's view is placed over firstController's view. The following code works:

CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;

secondController.view.frame = CGRectMake(width, 0, width, height);

[self transitionFromViewController:firstController
    toViewController:secondController
    duration:0.4
    options:UIViewAnimationOptionTransitionNone
    animations:^(void) {
        firstController.view.frame = CGRectMake(0 - width, 0, width, height);
        secondController.view.frame = CGRectMake(0, 0, width, height);
    } 
    completion:^(BOOL finished){
        [secondController didMoveToParentViewController:self]; 
    }
];
like image 104
Simple99 Avatar answered Nov 16 '22 01:11

Simple99