“从视图控制器"使用 UIViewControllerContextTransitioning 消失 [英] "From View Controller" disappears using UIViewControllerContextTransitioning

查看:17
本文介绍了“从视图控制器"使用 UIViewControllerContextTransitioning 消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我已经在下面进行了描述.

I got one problem and i have described it below.

我正在使用 UIViewControllerContextTransitioning 进行自定义转换.

I am using UIViewControllerContextTransitioning for custom transitions.

我有 2 个视图控制器,第一个视图控制器和第二个视图控制器.

I have 2 view controllers, first view controller and second view controller.

现在我想用动画在第一个视图控制器上添加第二个视图控制器.我已经实现了,现在第二个视图控制器是透明的,所以我们可以在第二个视图控制器下面看到第一个视图控制器.

Now I want to add second view controller on first view controller with an animation. I have achieved it, now the second view controller is transparent, so we can see first view controller below second view controller.

但是我看不到第一个视图控制器,我只能看到第二个视图控制器下方的黑屏.

But I am not able to see first view controller, and I can see only black screen below second view controller.

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    self.transitionContext = transitionContext;
    if(self.isPresenting){
        [self executePresentationAnimation:transitionContext];
    }
    else{
       [self executeDismissalAnimation:transitionContext];
    }
  }

-(void)executePresentationAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
     UIView* inView = [transitionContext containerView];
     UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

     UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

     CGRect offScreenFrame = inView.frame;
     offScreenFrame.origin.y = inView.frame.size.height;
     toViewController.view.frame = offScreenFrame;

    toViewController.view.backgroundColor = [UIColor clearColor];
    fromViewController.view.backgroundColor = [UIColor clearColor];
    inView.backgroundColor = [UIColor  clearColor];
    [inView insertSubview:toViewController.view aboveSubview:fromViewController.view];
     // [inView addSubview:toViewController.view];
    CFTimeInterval duration = self.presentationDuration;
    CFTimeInterval halfDuration = duration/2;

    CATransform3D t1 = [self firstTransform];
    CATransform3D t2 = [self secondTransformWithView:fromViewController.view];

    [UIView animateKeyframesWithDuration:halfDuration delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{

    [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.5f animations:^{
        fromViewController.view.layer.transform = t1;
    }];

    [UIView addKeyframeWithRelativeStartTime:0.5f relativeDuration:0.5f animations:^{
        fromViewController.view.layer.transform = t2;
    }];
    } completion:^(BOOL finished) {
    }];


    [UIView animateWithDuration:duration delay:(halfDuration - (0.3*halfDuration)) usingSpringWithDamping:0.7f initialSpringVelocity:6.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
        toViewController.view.frame = inView.frame;
    } completion:^(BOOL finished) {
        [self.transitionContext completeTransition:YES];
    }];
}

[self.transitionContext completeTransition:YES];调用时,第一个视图控制器突然消失,第二个视图控制器下方显示黑屏.

When [self.transitionContext completeTransition:YES]; called, suddenly the first view controller disappears and black screen displays below second view controller.

有人知道吗?谢谢.

推荐答案

我在这里遇到了同样的问题 – 看起来像是 iOS 8 中的一个错误.我已经提交了雷达.

I was having the same problem here – looks like a bug in iOS 8. I've filed a radar.

我使用 Reveal 在屏幕变黑后检查视图层次结构.键 UIWindow 完全是空的——根本没有视图层次结构!

I used Reveal to inspect the view hierarchy after the screen goes black. The key UIWindow is completely empty – no view hierarchy at all!

我玩了一会儿,看起来有一个简单的解决方法,适用于简单的情况.您可以重新添加 toViewController 的视图作为关键窗口的子视图:

I played around a bit and it looks like there is an easy workaround, for simple cases. You can just re-add the toViewController's view as a subview of the key window's:

transitionContext.completeTransition(true)
UIApplication.sharedApplication().keyWindow!.addSubview(toViewController.view)

我已经检查过,关键窗口的 rootViewController 仍然正确设置,所以没问题.我不确定如果您从已经呈现的模态控制器中呈现您的控制器会发生什么,因此对于更复杂的情况,您必须进行试验.

I've checked and the key window's rootViewController is still correctly set, so that's fine. I'm not sure what would happen if you presented your controller from within an already presented modal controller, so for more complex cases, you'll have to experiment around.

这篇关于“从视图控制器"使用 UIViewControllerContextTransitioning 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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