LetBulletFlies
LetBulletFlies

Reputation: 285

how to do animated transition between different UIViewControllers within a UITabBarController?

I have a UITabBarController,displaying 4 tabs.

I want to add an animated UIViewController transition when user swiping screen to switch tab. (- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController method only work for select tab directly). transition will be in easeinout style.

I tried following codes but not work. only coming UIViewController moves, going UIViewController doesn't show at all.(I printed all UIViewControllers' frame data, all these 4 UIViewControllers are {0,0},{320,480} )

// Get views. controllerIndex is passed. 
UIViewController *fromVC = [_tabBarController.viewControllers objectAtIndex:startIndex];
UIViewController *toVC = [_tabBarController.viewControllers objectAtIndex:to];

UIView *fromView = fromVC.view;
UIView *toView = toVC.view;

MWLog(@"from view is %@",fromView);

int direct = startIndex - to; 
// calculate move direction.
if (direct < 0) {

    CGRect outFrame = fromView.frame;
    outFrame.origin.x = -320; // expect fromView will move to {-320,0}, out of screen.
    MWLog(@"fromView's frame is %@", NSStringFromCGRect(fromView.frame));

    CGRect inFrame = toView.frame;
    inFrame.origin.x = 0;

    MWLog(@"toView's frame is %@", NSStringFromCGRect(toView.frame));

    [UIView beginAnimations:@"moveView" context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5f];

    toView.frame = inFrame;
    fromView.frame = outFrame;
    [UIView commitAnimations];
}else
{
    // reverse moving. ignored....

}

Could you tell me what's wrong and how to do it correctly? thanks in advance!

Upvotes: 0

Views: 1252

Answers (1)

LetBulletFlies
LetBulletFlies

Reputation: 285

finally I found the solution.

use CATransition animation, type push, type left or right depending on direction(by calculating tabbarcontroller's selectedIndex property). then, add this animation to tabbarcontroller's container view.

This view's reference can be got by enumerate all UIViews in [tabbarcontroller subviews] array. Actually, if no custom UIView, tabbarcontroller contains 2 subviews, one is UITabBar, the other is the container view, UITransitionView. Add animation on this view, you can enable page transition animation for different content screen.

Upvotes: 0

Related Questions