提供2个viewcontrollers,但只有一个转换

时间:2016-03-04 17:39:11

标签: ios uiviewcontroller

我想从View Controller" A",第二个ViewController" B" (带导航栏)应显示第三个模态ViewController" C",显示倒计时并自行解散,最终显示" B"。

我想" C"以模态呈现,没有" B"被人看到,当它被解雇时," B"应该已经在那里了。

我一直在看这篇文章:

但是因为我需要" B"被推(不是模态)我不能使用UIModalPresentationCurrentContext

我也试过这样做:How to push two view controllers but animate transition only for the second one?

这几乎是我想要的,但我喜欢" C"以模态样式呈现,例如封面垂直。

1 个答案:

答案 0 :(得分:2)

我最终最终与这两个人混在一起:

在vcB中:

    -(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
    {
        if (operation == UINavigationControllerOperationPush || operation == UINavigationControllerOperationPop) {
            MyAnimator *animator = [[MyAnimator alloc] init];
            animator.presenting = (operation == UINavigationControllerOperationPush);
            return animator;
        }
        return nil;
    }

在MyAnimator.m中:

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.4;
}

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

    [[transitionContext containerView] addSubview:toViewController.view];

    CGRect currentFrame = toViewController.view.frame;
    CGRect toFrameInitial = self.presenting ? CGRectOffset(currentFrame, 0, CGRectGetHeight(currentFrame)) : CGRectOffset(currentFrame, 0, -CGRectGetHeight(currentFrame));
    CGRect fromFrameFinal = self.presenting ? CGRectOffset(currentFrame, 0, -CGRectGetHeight(currentFrame)) : CGRectOffset(currentFrame, 0,   CGRectGetHeight(currentFrame));

    toViewController.view.frame = toFrameInitial;
    [UIView animateWithDuration:[self    transitionDuration:transitionContext] animations:^{
        toViewController.view.frame = currentFrame;
        fromViewController.view.frame = fromFrameFinal;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}