我想通過動畫代碼更改選項卡。確切的情況是,有2個選項卡具有以下層次結構。以動畫方式編程更改tabbarController的選項卡
First tab
- Navigation controller
- Login controller
- Some other controller
Second tab
- Navigation controller
- Screen with Logout button
現在,如果用戶按下注銷,我需要顯示登錄屏幕。爲此,我需要切換標籤FirstTab
,然後popToRootViewController。
所以我在做什麼是退出按鈕按下我發送NSNotification
到LoginController
,然後執行下面的方法。
- (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;
}
http://stackoverflow.com/questions/5161730/iphone-how-to-switch-tabs-with-an-animation –
@ Vijay-Apple-Dev.blogspot.com請看我更新的問題。 – Geek
嘗試我的實現並讓我知道 –