【问题标题】:Change tabbarController's tab programmatically with animation使用动画以编程方式更改 tabbarController 的选项卡
【发布时间】: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


【解决方案1】:

下面是我用来为具有滑入滑出效果的屏幕制作动画的代码。发现于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;
        }
    }];
}

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 这行得通。但我想用默认动画制作动画,而不是翻转。
    • 你想要什么样的动画?
    • iOS 默认动画。即从右到左的过渡。换句话说,弹出视图控制器时发生的动画。
    【解决方案3】:

    对不起 Geek,我周末断线了。

    我在我的应用程序中采用的解决方案是创建一个调用popToRootViewControllerAnimated: 的方法,然后将performSelector: 消息发送到self,将创建的方法作为选择器传递:

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

    也许它不是最好的和执行良好的解决方案,但它是一种选择,因为做这项工作。它将选择选项卡,并在 1.5 秒延迟后播放动画。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2016-12-03
      • 2021-12-08
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多