【问题标题】:Present 2 viewcontrollers but only one transition呈现 2 个视图控制器,但只有一个过渡
【发布时间】:2016-03-04 17:39:11
【问题描述】:

我想从视图控制器“A”、第二个视图控制器“B”(带导航栏)推送,它应该显示第三个模式视图控制器“C”,它显示倒计时并自行关闭,最终显示“B ”。

我希望以模态方式呈现“C”,而不会看到“B”,当它被解除时,“B”应该已经存在了。

我一直在看这个帖子:

但由于我需要推送“B”(不是模态的)我不能使用UIModalPresentationCurrentContext

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

这几乎是我想要的,但我希望“C”以模态样式呈现,例如垂直封面。

【问题讨论】:

    标签: ios uiviewcontroller


    【解决方案1】:

    我终于把这两个混合在一起了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2019-05-26
      • 2017-02-03
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多