我想通过动画代码更改选项卡。确切的情况是,有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
尝试我的实现并让我知道 –