2014-05-02 78 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秒後,動畫將發生。希望能幫助到你。

相關問題