我有一个 AccountsViewController,它的 UIVisualEffectView
在背景中具有模糊效果(索引 0 处的 subview ),因此它在过渡期间用模糊覆盖了之前的 Controller 。但是当我切换到另一个应用程序并再次打开多任务菜单以切换回我的应用程序时,其模糊的 UIVisualEffectView 似乎已损坏(如屏幕截图 1 所示)。当我再次激活我的应用程序时, View 得到“修复”并且再次看起来正常(如第二个屏幕截图所示)
我添加模糊 View 的自定义过渡类中的一些代码
// in the animateTransition.. method
guard let container = transitionContext.containerView() else {
return
}
let blurEffectView: UIVisualEffectView
if container.viewWithTag(101) != nil {
blurEffectView = container.viewWithTag(101)! as! UIVisualEffectView
}
else {
blurEffectView = UIVisualEffectView(frame: container.frame)
blurEffectView.translatesAutoresizingMaskIntoConstraints = false
blurEffectView.tag = 101
container.addSubview(blurEffectView)
}
if self.isPresenting {
// We're presenting a view controller over another
toViewController.view.transform = CGAffineTransformMakeScale(0.7, 0.7)
container.addSubview(toViewController.view)
container.sendSubviewToBack(blurEffectView)
toViewController.view.alpha = 0
UIView.animateWithDuration(self.transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: { () -> Void in
fromViewController.view.transform = CGAffineTransformMakeScale(0.8, 0.8)
toViewController.view.transform = CGAffineTransformMakeScale(1, 1)
blurEffectView.effect = UIBlurEffect(style: .Dark)
toViewController.view.alpha = 1
}, completion: { (completed) -> Void in
self.context?.completeTransition(completed)
})
}
else {
// We're dismissing a view controller
UIView.animateWithDuration(self.transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: { () -> Void in
fromViewController.view.transform = CGAffineTransformMakeScale(0.7, 0.7)
toViewController.view.transform = CGAffineTransformMakeScale(1, 1)
blurEffectView.effect = nil
fromViewController.view.alpha = 0
}, completion: { (completed) -> Void in
fromViewController.view.removeFromSuperview()
self.context?.completeTransition(completed)
})
}
最佳答案
似乎我自己已经弄明白了:这个问题只有在有一个 UIWindow 背面(黑色)显示出来时才会出现。 在这个问题中,我有一个带有这些 View 的过渡动画容器:
- 来自 ViewController 的 View (比例转换为 0.8;0.8)
- 应用了 .Dark 风格模糊的 UIVisualEffectView
- 到ViewController的 View
由于 fromViewController 的 View 大小(由于比例变换,它比屏幕小),黑色的 UIWindow View 被显示出来。根据我的研究,这会导致 UIVisualEffectView 产生这个问题中描述的故障。
解决方案是添加一个带有 UIWindow 框架的黑色 UIView,并将其添加到容器 View 层次结构的底部:
let blackView = UIView(frame: container.frame)
blackView.backgroundColor = UIColor.blackColor()
container.insertSubview(blackView, atIndex: 0)
然后这个故障似乎不再出现了。
关于swift - 多任务处理中奇怪的 UIVisualEffectView 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120685/