我四处寻找这个问题的答案,并且在过去的两个小时里一直在绞尽脑汁。
我正在实现一个非常基本的自定义 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/