使用animation以编程方式更改tabbarController的选项卡

我想通过animation代码更改选项卡。 确切的情况是,有2个选项卡与下面的层次结构。

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

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

所以我在做什么是在退出button按我发送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; } 

我尝试下面的方法来animation。 但是,这只有当用户更改制表符时,而不是通过代码更改时,animation。

 - (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; } 

以下是我用来为屏幕添加幻灯片效果的代码。 在SO问题上find。

 - (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; } }]; } 

尝试这个

 - (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; }]; } 

我很抱歉,极客,周末我断线了。

我在我的应用程序中的解决scheme是调用popToRootViewControllerAnimated:然后发送一个performSelector:消息self传递创build的方法作为select器:

 - (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]; 

也许这不是最好的,performance良好的解决scheme,但这是一个select,因为这样做的工作。 它会select标签,并在1.5秒的延迟后,animation将发生。 希望能帮助到你。