ios – 容器视图在completeTransition上消失:

前端之家收集整理的这篇文章主要介绍了ios – 容器视图在completeTransition上消失:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用自定义视图控制器转换UIViewControllerAnimatedTransitioning来呈现和关闭视图控制器.

演示动画工作正常,但是当我运行关闭动画时,一旦我调用completeTransition:containerView被删除.

我不知道发生了什么,这里是转换代码

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

    UIView *containerView = [transitionContext containerView];
    containerView.backgroundColor = [UIColor blackColor];

    if (self.reverse) {
        [containerView addSubview:toViewController.view];
        [containerView addSubview:fromViewController.view];
    } else {
        [containerView addSubview:fromViewController.view];
        [containerView addSubview:toViewController.view];
    }

    if (! self.reverse) { // Forward
        toViewController.view.frame = CGRectMake(-containerView.frame.size.width,containerView.frame.size.width,containerView.frame.size.height);
    } else {
        fromViewController.view.frame = CGRectMake(0,containerView.frame.size.height);
    }

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
        if (self.reverse) {
            fromViewController.view.frame = CGRectMake(-containerView.frame.size.width,containerView.frame.size.height);
            fromViewController.view.layer.opacity = 0.f;
            toViewController.view.layer.opacity = 1.f;
        } else {
            toViewController.view.frame = CGRectMake(0,containerView.frame.size.height);
            toViewController.view.layer.opacity = 1.f;
            fromViewController.view.layer.opacity = 0.3f;
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:finished];
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.reverse) {
        return 0.45;
    } else {
        return 0.35;
    }
}

如果.reverse设置为YES,可以如何防止我的toViewController消失?

更新:这是我如何呈现视图控制器:

SecondaryViewController *vc = [[SecondaryViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.modalPresentationStyle = UIModalPresentationCustom;
navigationController.transitioningDelegate = self;
[self presentViewController:navigationController animated:YES completion:nil];

解决方法

容器视图在解雇时消失是正确的行为.你的错误是将fromView添加到它.

您是否正确地区分这是呈现还是解雇,以及您应该在每种情况下做什么.只需使用来自ViewController和ToViewController的两个视图控制器将它们分开;解雇后,角色被扭转了.解雇时,不要对内容视图添加任何内容;原始主持人仍然存在,并将通过清除集装箱视图来显示.

因此,在演示文稿中,只将toView添加到容器视图.解雇时,不要在容器视图中添加任何内容.就这么简单.

原文链接:https://www.f2er.com/iOS/336835.html

猜你在找的iOS相关文章