容器视图在completeTransition上消失: [英] Container view disappearing on completeTransition:

查看:613
本文介绍了容器视图在completeTransition上消失:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义视图控制器转换 UIViewControllerAnimatedTransitioning 来显示和关闭视图控制器。



呈现动画效果很好,但是当我运行dismiss动画时,一旦我调用 completeTransition: containerView 就会被删除。 / p>

我不确定发生了什么,这里是转换代码:

   - (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){//转发
toViewController.view.frame = CGRectMake(-containerView.frame.size.width,0,containerView.frame .size.width,containerView.frame.size.height);
} else {
fromViewController.view.frame = CGRectMake(0,0,containerView.frame.size.width,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,0,containerView.frame.size.width,containerView.frame.size.height);来自ViewController.view.layer.opacity = 0.f的
;
toViewController.view.layer.opacity = 1.f;
} else {
toViewController.view.frame = CGRectMake(0,0,containerView.frame.size.width,containerView.frame.size.height);
toViewController.view.layer.opacity = 1.f;
fromViewController.view.layer.opacity = 0.3f;
}
}完成:^(BOOL完成){
[transitionContext completeTransition:finished];
}];
}

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

如何阻止我的 toViewController 如果 .reverse 设置为 YES

$ b,则消失
$ b

更新:这就是我呈现视图控制器的方式:

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


解决方案

解雇时消失的容器视图是正确的行为。你的错误是将从View 添加到它。



你错误地区分这是演示还是解雇以及你是什么应该做每种情况。只需使用两个视图控制器 fromViewController toViewController 来区分它们;在解雇时,角色是相反的。解雇时,不要向内容视图添加任何内容;原始演示者仍然存在并将通过删除容器视图显示。



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


I am using custom view controller transitions, UIViewControllerAnimatedTransitioning, to present and dismiss a view controller.

The presenting animation works fine, but when I run the dismiss animation, once I call completeTransition: the containerView gets removed.

I'm not sure what is going on, here is the transition code:

- (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, 0, containerView.frame.size.width, containerView.frame.size.height);
    } else {
        fromViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, 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, 0, 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, 0, containerView.frame.size.width, 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;
    }
}

How can I prevent my toViewController from disappearing if .reverse is set to YES?

Update: This is how I'm presenting the view controller:

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

解决方案

The container view disappearing on dismissal is correct behavior. Your mistake is adding the fromView to it.

You are incorrectly distinguishing whether this is presentation or dismissal and what you should do in each case. Simply use the two view controllers fromViewController and toViewController to tell them apart; on dismissal, the roles are reversed. On dismissal, do not add anything to the content view; the original presenter is still present and will be revealed by the removal of the container view.

So, on presentation, add only the toView to the container view. On dismissal, do not add anything to the container view. It's as simple as that.

这篇关于容器视图在completeTransition上消失:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆