使用自定义UIViewController动画制作动画后,呈现的视图控制器消失 [英] Presented view controller disappears after animation using custom UIViewController animations

查看:80
本文介绍了使用自定义UIViewController动画制作动画后,呈现的视图控制器消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案,在过去的两个小时中,我一直拉着头发.

I've looked around for an answer for this and spent the last two hours pulling my hair out to no end.

我正在实现一个非常基本的自定义视图控制器过渡动画,该动画仅放大呈现的视图控制器并在呈现的视图控制器中进行扩展.它增加了淡入淡出的效果(0至1的alpha值,反之亦然).

I'm implementing a very basic custom view controller transition animation, which simply zooms in on the presenting view controller and grows in the presented view controller. It adds a fade effect (0 to 1 alpha and visa versa).

在显示视图控制器时它可以正常工作,但是在关闭视图控制器时,它会一直将presenting视图控制器带回整个屏幕,但是随后莫名其妙地消失了.在这些动画改变了Alpha或隐藏值之后,我什么也不做,这几乎是一个新鲜项目.我已经开发iOS应用程序3年了,所以我怀疑这可能是个错误,除非有人可以找出我要去哪里.

It works fine when presenting the view controller, however when dismissing, it brings the presenting view controller back in all the way to fill the screen, but then it inexplicably disappears. I'm not doing anything after these animations to alter the alpha or the hidden values, it's pretty much a fresh project. I've been developing iOS applications for 3 years so I suspect this may be a bug, unless someone can find out where I'm going wrong.

class FadeAndGrowAnimationController : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController!, presentingController presenting: UIViewController!, sourceController source: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
    return self
}

func animationControllerForDismissedController(dismissed: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
    return self
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {
    return 2
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as UIViewController
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as UIViewController

    toViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
    toViewController.view.alpha = 0

    transitionContext.containerView().addSubview(fromViewController.view)
    transitionContext.containerView().addSubview(toViewController.view)
    transitionContext.containerView().bringSubviewToFront(toViewController.view)

    UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
        fromViewController.view.transform = CGAffineTransformScale(fromViewController.view.transform, 2, 2)
        fromViewController.view.alpha = 1

        toViewController.view.transform = CGAffineTransformMakeScale(1, 1)
        toViewController.view.alpha = 1
    }, completion: { finished in
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    })
}

}

要显示的代码:

    let targetViewController = self.storyboard.instantiateViewControllerWithIdentifier("Level1ViewController") as Level1ViewController
    let td = FadeAndGrowAnimationController()

    targetViewController.transitioningDelegate = td
    targetViewController.modalPresentationStyle = .Custom

    self.presentViewController(targetViewController, animated: true, completion: nil)

如您所见,这是一个相当基本的动画.我在这里想念什么吗?就像我说的,它呈现完美,然后关闭99.99%完美,但是解雇后的下方视图控制器被莫名其妙地删除了.发生这种情况后,iPad会显示黑屏-全黑.

As you can see, a fairly basic animation. Am I missing something here? Like I said, it presents perfectly fine, then dismisses 99.99% perfectly fine, yet the view controller underneath after the dismissal is inexplicably removed. The iPad shows a blank screen - totally black - after this happens.

推荐答案

这似乎是一个iOS8错误.我找到了解决方案,但它是贫民窟.过渡后,当视图应显示在屏幕上但不在屏幕上时,则需要将其添加回窗口,如下所示:

This seems to be an iOS8 bug. I found a solution but it is ghetto. After the transition when a view should be on-screen but isn't, it needs to be added back to the window like this:

BOOL canceled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!canceled];
if (!canceled)
    {
    [[UIApplication sharedApplication].keyWindow addSubview: toViewController.view];
    }

您可能需要试一下添加回窗口的视图,是取消还是取消,并确保仅在解雇而不是在演示时进行.

You might need to play around with which view you add back to the window, whether to do it in canceled or !canceled, and perhaps making sure to only do it on dismissal and not presentation.

来源:容器视图在completeTransition上消失了: http://joystate.wordpress.com/2014 /09/02/ios8-and-custom-uiviewcontrollers-transitions/

这篇关于使用自定义UIViewController动画制作动画后,呈现的视图控制器消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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