【发布时间】:2014-05-02 09:55:26
【问题描述】:
我想通过带有动画的代码更改选项卡。确切的情况是有 2 个选项卡具有以下层次结构。
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;
}
【问题讨论】:
-
@Vijay-Apple-Dev.blogspot.com 请查看我更新的问题。
-
试试我的实现并告诉我
-
您是否尝试在呼叫
popToRootViewControllerAnimated:后设置延迟,可能是 1 或 2 秒延迟?我已经用我的应用程序做到了,并且运行良好。我的意思是,首先选择选项卡,然后延迟调用该方法。这是一种选择。 -
@ThXou 你能发布你的代码吗?
标签: ios objective-c ios7 uitabbarcontroller core-animation