使用动画以编程方式更改tabbarController的选项卡 [英] Change tabbarController's tab programmatically with animation

查看:61
本文介绍了使用动画以编程方式更改tabbarController的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过动画代码更改标签。确切的情况是有2个以下层次结构的标签。

I want to change tab through code with animation. Exact scenario is that there are 2 tabs with below hierarchy.

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

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

Now, if user presses logout, I need to display login screen. For that I need to switch tab to FirstTab and then popToRootViewController.

所以我正在做的是登出按钮按我发送 NSNotification LoginController 然后执行以下方法。

So what I am doing is on logout button press I send NSNotification to LoginController which in turn executes below method.

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

我尝试使用以下方法制作动画。但是只有当用户更改了tab而不是通过代码更改时,这才会动画。

I tried below method to animate. But this animates only when tab is changed by user but not when changed through code.

- (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问题上找到。

Below is the code I used to animate screens with slide in-out effect. Found on 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;
        }
    }];
}

这篇关于使用动画以编程方式更改tabbarController的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆