简体   繁体   English

使用UITabBarControllerDelegate进行UIViewController过渡

[英]UIViewController transition using UITabBarControllerDelegate

I've got a UITabBarControllerDelegate set to itself for a UITabBarController. 我为UITabBarController设置了一个UITabBarControllerDelegate。 I just want to animate the transition for the UITabBarController child view controllers. 我只想为UITabBarController子视图控制器的过渡设置动画。

I'm using these methods from UITabBarControllerDelegate and UIViewControllerAnimatedTransitioning: 我正在使用UITabBarControllerDelegate和UIViewControllerAnimatedTransitioning中的这些方法:

- (id<UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    return self;
}


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];

    [container addSubview:toViewController.view];
    [container addSubview:fromViewController.view];

    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;

    if ([[toViewController valueForKey:@"title"] isEqualToString:@"viewControllerTitle"]) {
        [container bringSubviewToFront:toViewController.view];

        [fromViewController.view setFrame:CGRectMake(0, 0, 320, screenHeight)];
        [toViewController.view setFrame:CGRectMake(320, 0, 320, screenHeight)];

        [UIView animateWithDuration:0.3 animations:^{
            [toViewController.view setFrame:CGRectMake(0, 0, 320, screenHeight)];
        }
                         completion:^(BOOL finished){
                             [transitionContext completeTransition:finished];
                         }];
    }
}

Every lines of code above are being hit, but the result is just one black screen behind the UITabBar at the bottom, and if I remove the UITabBarControllerDelegate method to not call the transitioning context code, everything works fine. 上面的每一行代码都被命中,但是结果仅是底部UITabBar后面一个黑屏,如果我删除UITabBarControllerDelegate方法以不调用过渡上下文代码,则一切正常。

I'm using a similar transitioning animation code for another part of my app that's just called from a UIViewController.transitioningDelegate instead of a UITabBarController.delegate and it works fine, so I think something is different about how UITabBarControllerDelegate handles transitions. 我正在从UIViewController.transitioningDelegate而不是UITabBarController.delegate调用我的应用程序的另一部分使用类似的过渡动画代码,并且效果很好,所以我认为UITabBarControllerDelegate处理过渡的方式有所不同。

Can someone see why it gives me that black screen? 有人可以看到为什么它给了我黑屏吗?

Thanks in advance! 提前致谢!

You could try something like that. 您可以尝试类似的方法。 There is also a question about that already answered: answer 还有一个关于已经回答的问题: 答案

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSArray *tabViewControllers = tabBarController.viewControllers;
    UIView * fromView = tabBarController.selectedViewController.view;
    UIView * toView = viewController.view;
    if (fromView == toView)
        return;
    NSUInteger fromIndex = [tabViewControllers indexOfObject:tabBarController.selectedViewController];
    NSUInteger toIndex = [tabViewControllers indexOfObject:viewController];

    [UIView transitionFromView:fromView
                        toView:toView
                      duration:0.3
                       options: UIViewAnimationOptionTransitionFlipFromRight
                    completion:^(BOOL finished) {
                        if (finished) {
                            tabBarController.selectedIndex = toIndex;
                        }
                    }];
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM