Change tabbarController's tab programmatically with animation

我是研究僧i 提交于 2019-12-01 08:45:57
Geek

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

try this

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

                    }];


}

I'm Sorry Geek, I was disconnected on weekend.

The solution that I took in my app was make a method which call to the popToRootViewControllerAnimated: and then send a performSelector: message to self passing that created method as a selector:

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

Maybe it is not the best and well performed solution, but it's an option because do the work. It will select the tab and after a 1.5 second delay, the animation will take place. Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!