ios - 呈现的 View Controller 在使用自定义 UIViewController 动画制作动画后消失

标签 ios ipad animation swift

我四处寻找这个问题的答案,并且在过去的两个小时里一直在绞尽脑汁。

我正在实现一个非常基本的自定义 View Controller 转换动画,它只是在呈现的 View Controller 上放大并在呈现的 View Controller 中增长。它添加了淡入淡出效果(0 到 1 alpha,反之亦然)。

它在呈现 View Controller 时工作正常,但在关闭时,它会将 presenting View Controller 带回原处以填满屏幕,但随后它莫名其妙地消失了。在这些动画之后我没有做任何改变 alpha 或隐藏值的事情,这几乎是一个新项目。我已经开发 iOS 应用程序 3 年了,所以我怀疑这可能是一个错误,除非有人能找出我哪里出错了。

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%,但是在关闭之后下面的 View Controller 莫名其妙地被删除了。发生这种情况后,iPad 显示空白屏幕 - 全黑。

最佳答案

这似乎是一个 iOS8 错误。我找到了一个解决方案,但它是贫民窟。当一个 View 应该在屏幕上但不在屏幕上时,转换之后,需要像这样将它添加回窗口:

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

您可能需要考虑将哪个 View 添加回窗口,是否在取消或 !canceled 中执行,并且可能确保仅在关闭时执行,而不是在演示时执行。

来源:Container view disappearing on completeTransition: http://joystate.wordpress.com/2014/09/02/ios8-and-custom-uiviewcontrollers-transitions/

关于ios - 呈现的 View Controller 在使用自定义 UIViewController 动画制作动画后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25618322/

相关文章:

css - @keyframes 在 Firefox 或 IE 中不工作。仅适用于 Chrome

animation - 如何设置 EaselJS 图形和形状的 z-index

iphone - 如何在 iOS6 中制作与 iOS5 相同的 map

html - 带动画的中央分割布局(仅 CSS)

iphone - UIWebView背景设置为Clear Color,但不透明

ios: mapkit 如何在 map 上启用当前位置按钮?

iOS 下载 mp3 稍后在应用程序中使用

iphone - UITableViewCell 背景颜色?

ios - 了解如何使用 Objective-C 在 iOS 上制作简单的数学游戏

ios - iOS Vision 框架面部跟踪的高 CPU 和能源使用