2014-05-02 79 views
3

我想通过动画代码更改选项卡。确切的情况是,有2个选项卡具有以下层次结构。以动画方式编程更改tabbarController的选项卡

First tab 
    - Navigation controller 
    - Login controller 
    - Some other controller 
Second tab 
    - Navigation controller 
    - Screen with Logout button 

现在,如果用户按下注销,我需要显示登录屏幕。为此,我需要切换标签FirstTab,然后popToRootViewController。

所以我在做什么是退出按钮按下我发送NSNotificationLoginController,然后执行下面的方法。

- (void)logoutButtonPressed 
{ 
    // Go to root controller in navigation controller of first tab. 
    [self.navigationController popToRootViewControllerAnimated:YES]; 

    // Change tab to "First tab". This happens sharply without animation. 
    // I want to animate this change. 
    self.tabBarController.selectedIndex = 0; 
} 

我试过下面的方法来动画。但是,只有当用户更改了选项卡时才会生成动画,但通过代码更改时不会生成动画。

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{ 
    NSArray *tabViewControllers = tabBarController.viewControllers; 
    UIView * fromView = tabBarController.selectedViewController.view; 
    UIView * toView = viewController.view; 
    if (fromView == toView) 
     return false; 
    NSUInteger fromIndex = [tabViewControllers indexOfObject:tabBarController.selectedViewController]; 
    NSUInteger toIndex = [tabViewControllers indexOfObject:viewController]; 

    [UIView transitionFromView:fromView 
         toView:toView 
         duration:0.3 
         options: toIndex > fromIndex ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight 
        completion:^(BOOL finished) { 
         if (finished) { 
          tabBarController.selectedIndex = toIndex; 
         } 
        }]; 

    return true; 
} 
+0

http://stackoverflow.com/questions/5161730/iphone-how-to-switch-tabs-with-an-animation –

+0

@ Vijay-Apple-Dev.blogspot.com请看我更新的问题。 – Geek

+0

尝试我的实现并让我知道 –

回答

2

下面是我用来制作动画幻灯片与在出效果的屏幕的代码。发现于SO question

- (void)logoutButtonPressed 
{ 
    [self.navigationController popToRootViewControllerAnimated:NO]; 

    [self animateTransitionBetweenControllers]; 
} 

// Animates view transition that happens from screen with logout button to login screen 
- (void)animateTransitionBetweenControllers 
{ 
    // Get the views to animate. 
    UIView * fromView = self.tabBarController.selectedViewController.view; 
    UIView * toView = [[self.tabBarController.viewControllers objectAtIndex:0] view]; 

    // Get the size of the view. 
    CGRect viewSize = fromView.frame; 

    // Add the view that we want to display to superview of currently visible view. 
    [fromView.superview addSubview:toView]; 

    // Position it off screen. We will animate it left to right slide. 
    toView.frame = CGRectMake(-self.view.bounds.size.width, viewSize.origin.y, toView.bounds.size.width, viewSize.size.height); 

    // Animate transition 
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{ 
     // Animate the views with slide. 
     fromView.frame = CGRectMake(self.view.bounds.size.width, viewSize.origin.y, toView.bounds.size.width, viewSize.size.height); 
     toView.frame = CGRectMake(0, viewSize.origin.y, toView.bounds.size.width, viewSize.size.height); 
    } completion:^(BOOL finished) { 
     if (finished) 
     { 
      // Remove the old view. 
      [fromView removeFromSuperview]; 
      self.tabBarController.selectedIndex = 0; 
     } 
    }]; 
} 
1

试试这个

- (void)logoutButtonPressed 
{ 
    // Go to root controller in navigation controller of first tab. 
    [self.navigationController popToRootViewControllerAnimated:YES]; 


    NSArray *tabViewControllers = self.tabBarController.viewControllers; 


    UIView * fromView = self.tabBarController.selectedViewController.view; 


    UIView * toView = self.view; 

    NSUInteger fromIndex = [tabViewControllers indexOfObject:self.tabBarController.selectedViewController]; 


    NSUInteger toIndex = [tabViewControllers indexOfObject:self]; 

    [UIView transitionFromView:fromView 
         toView:toView 
         duration:0.3 
         options: toIndex > fromIndex ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight 
        completion:^(BOOL finished) { 

          self.tabBarController.selectedIndex = 0; 

        }]; 


} 
+0

@Geek代码已更新 –

+0

这有效。但我想用默认动画制作动画,而不是翻转。 – Geek

+0

你想要什么样的动画? –

0

对不起,极客,我在周末断开连接。

,我把我的应用程序的解决方案是使该打电话到popToRootViewControllerAnimated:,然后发送performSelector:消息self通过创建方法作为选择的方法:

- (void)delayPopAnimation { 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

// in logoutButtonPressed make some like this 
self.tabBarController.selectedIndex = 0; 
[self performSelector:@selector(delayPopAnimation) withObject:nil afterDelay:1.5]; 

也许这是不是最好的和良好的执行解决方案,但这是一个选择,因为做这项工作。它会选择该选项卡,延迟1.5秒后,动画将发生。希望能帮助到你。

相关问题