使用UITabBarControllerDelegate进行UIViewController转换

时间:2014-07-24 11:01:51

标签: ios objective-c uiviewcontroller uitabbarcontroller transitions

我为UITabBarController设置了一个UITabBarControllerDelegate。我只是想为UITabBarController子视图控制器的转换设置动画。

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

上面的每行代码都被命中,但结果只是底部UITabBar后面的一个黑屏,如果我删除UITabBarControllerDelegate方法不调用转换上下文代码,一切正常。

我在我的应用程序的另一部分使用了类似的转换动画代码,它只是从UIViewController.transitioningDelegate调用而不是UITabBarController.delegate,它运行正常,所以我觉得有些不同UITabBarControllerDelegate如何处理转换。

有人能看出为什么它会给我黑屏吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

你可以尝试类似的东西。还有一个问题已经回答: 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;
}