Leon Lucardie
Leon Lucardie

Reputation: 9730

Animating UITabBarController navigation

I'm currently having an issue with iOS development regarding animation. I'm currently using the following code to make a "slide animation" for switching tab bar items after a gesturerecognizer fires.

-(void)slideToTab:(int)controllerIndex
{
    if(controllerIndex >= 0 && controllerIndex < [self.tabBarController.viewControllers count])
    {
        // Get the views.
        UIView * fromView = self.tabBarController.selectedViewController.view;
        UIView * toView = [[self.tabBarController.viewControllers objectAtIndex:controllerIndex] view];

        // Get the size of the view area.
        CGRect viewSize = fromView.frame;
        BOOL scrollRight = controllerIndex > self.tabBarController.selectedIndex;

        // Add the to view to the tab bar view.
        [fromView.superview addSubview:toView];

        // Position it off screen.
        toView.frame = CGRectMake((scrollRight ? 320 : -320), viewSize.origin.y, 320, viewSize.size.height);

        [UIView animateWithDuration:0.3
                         animations: ^{

                             // Animate the views on and off the screen. This will appear to slide.
                             fromView.frame =CGRectMake((scrollRight ? -320 : 320), viewSize.origin.y, 320, viewSize.size.height);
                             toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
                         }

                         completion:^(BOOL finished) {
                             if(finished)
                             {
                              // Remove the old view from the tabbar view.
                              [fromView removeFromSuperview];
                              self.tabBarController.selectedIndex = controllerIndex;
                             }
                         }];
    }
}

For the tab bar items on the tab bar this code works fine, but whenever I hit the first tab that's located on the "More" section of the tab bar, the animation stops working and the finished boolean in the completion block returns false. Is there a reason why this could happen in the "More" section of a tab bar and what would be a possible solution for this?

Upvotes: 2

Views: 2685

Answers (1)

Adlai Holler
Adlai Holler

Reputation: 840

Implementing custom tab bar transitions isn't officially supported under iOS 6, but under iOS 7 you can have your tab bar's delegate implement

-tabBarController:animationControllerForTransitionFromViewController:toViewController:

and return an object that implements

-transitionDuration and -animateTransition:

that will execute the transition.

Upvotes: 3

Related Questions