Search code examples
objective-cuiviewcontrollercore-animation

Handle default animation block when rotate UIViewController


I have an UIViewController into an navigationController.All I want is when I rotate from landscape into portrait, to change the view into another one which belong to another view controller(another tab from the bottom tab bar controller). I have done this using the following code:

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

  if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){  
      UIViewController *controller =  [[[UIApplication sharedApplication] delegate].tabBarController.viewControllers objectAtIndex : 1];
      [UIView beginAnimations: nil context: nil];
      [[self view] setAlpha:0.0];
      [[[UIApplication sharedApplication] delegate].tabBarController setSelectedViewController:controller];
      [UIView commitAnimations];  

  } 

}

All just fine, but I want to control the views switching like this: -when rotating, I want the current view to dissapear and the second one to appear(like fading in). - this before the actually rotation to take place.

The problem is that the current view do not dissapear immediately, it persist for about 0.2 seconds in the rotation process.

[[self view] setAlpha:0.0]; does not work here - I guess is modified again to be visible by the defalut animation block which is proceed by every View when rotating it.

Could somebody give me a solution?

Appreciate, Alex.


Solution

  • Did you try using delegates/blocks and doing the tabBar operation in the block? For example:

            [UIView animateWithDuration:0.1
                                  delay:0.0
                                options:UIViewAnimationOptionBeginFromCurrentState 
                             animations:^{ [self view].alpha = 0.0; } 
                             completion:^(BOOL fin) { 
                                 if (fin) {
                                     [[[UIApplication sharedApplication] delegate].tabBarController setSelectedViewController:controller];
                                 }
                             }];
    

    Note: haven't tested the code myself.

    For more on blocks and animation, you should check out Stanford's iPhone development course (it's free online) by Googling 'CS193p.'