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

时间:2014-05-02 09:55:27

标签: ios objective-c ios7 uitabbarcontroller core-animation

我想通过带动画的代码更改制表符。确切的情况是有2个标签低于层次结构。

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

现在,如果用户按下注销,我需要显示登录屏幕。为此,我需要将tab切换到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;
}

3 个答案:

答案 0 :(得分:2)

以下是我用于为具有幻灯片输入效果的屏幕设置动画的代码。找到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;
        }
    }];
}

答案 1 :(得分:1)

试试这个

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

                    }];


}

答案 2 :(得分:0)

我很抱歉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秒延迟后,将进行动画。希望它有所帮助。

相关问题