我以模态的方式呈现了一个具有模糊背景效果的视图控制器。iOS 10/XCode8引入了我的动画的一个问题。以下是演示文稿代码:
let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
modalVC.modalTransitionStyle = .CrossDissolve
modalVC.modalPresentationStyle = .OverFullScreen
presentViewController(modalVC, animated: true, completion: nil)在ModalViewController中添加viewDidLoad()模糊函数:
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.addSubview(blurEffectView)
view.sendSubviewToBack(blurEffectView)ModalViewController有一个清晰的背景,我添加了一个具有暗模糊效果的BlurEffectView。使用前面的代码段和在界面生成器中以编程方式进行了尝试。
在iOS 8和9上,.CrossDissolve转换处理了“淡入淡出”,但在iOS 10 (设备和模拟器)上测试后,视图显示为深色半透明背景色,而不是模糊。
.CrossDissolve动画完成后,背景颜色将更改为实际的模糊效果背景。你知道为什么会发生这种情况吗?
我还尝试在viewDidLoad()的开头和结尾为模式视图控制器添加layoutIfNeeded(),但没有成功。我使用的是swift 2.3
发布于 2017-03-21 00:39:12
嗨,
您需要创建新的UIViewControllerAnimatedTransitioning。
然后在animateTransition(using transitionContext: UIViewControllerContextTransitioning)中,你需要编写你的动画。
现在,在iOS 10中,您可以使用UIViewPropertyAnimator来为UIVisualBlurEffect的BlurRadius设置动画。
结果:

下面是一个用法示例:https://github.com/PierrePerrin/PPBlurModalPresentation
第一
你需要创建你的模糊过渡
class BlurModalPresentation: NSObject,UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
return 0.5
}
//This is the blur view used for transition
var blurView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.light))
var destinationView : UIView!
var animator: UIViewPropertyAnimator?
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
let containerView = transitionContext.containerView
_ = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
let toVc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
destinationView = toVc!.view
destinationView.alpha = 0.0
//Here we add the blur view and set it effect to nil
blurView.effect = nil
blurView.frame = containerView.bounds
self.blurTransition(transitionContext) {
self.unBlur(transitionContext, completion: {
self.blurView.removeFromSuperview()
transitionContext.completeTransition(true)
})
}
containerView.addSubview(toVc!.view)
containerView.addSubview(blurView)
}
//This methods add the blur to our view and our destinationView
func blurTransition(_ context : UIViewControllerContextTransitioning,completion: @escaping () -> Void){
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: self.transitionDuration(using: context)/2, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.destinationView.alpha = 0.5
self.blurView.effect = UIBlurEffect(style: UIBlurEffectStyle.light)
}, completion: { (position) in
completion()
})
}
//This Method remove the blur view with an animation
func unBlur(_ context : UIViewControllerContextTransitioning,completion: @escaping () -> Void){
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: self.transitionDuration(using: context) / 2, delay:0, options: UIViewAnimationOptions.curveLinear, animations: {
self.destinationView.alpha = 1.0
self.blurView.effect = nil
}, completion: { (position) in
completion()
})
}
}然后
您需要在ViewController中设置转换委托
import UIKit
class ViewController: UIViewController,UIViewControllerTransitioningDelegate {
let blurModalPresentation = BlurModalPresentation()
override func viewDidLoad() {
super.viewDidLoad()
}
func showVC(){
let str = self.storyboard!
let vc = str.instantiateViewController(withIdentifier: "YourViewControllerIdentifier")
vc.transitioningDelegate = self
self.present(vc, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
return blurModalPresentation
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
return blurModalPresentation
}
}发布于 2016-09-26 16:46:19
首先,我认为这个解决方案是一个临时的解决方案,因为我必须假设这个新引入的行为是一个错误,并将在未来的更新中修复。这使得它稍微不那么明显,因为模糊效果在动画期间而不是在动画之后弹出。仍然不如iOS 9和以后的版本好,但略好一些。
不带动画的
presentViewController(modalVC,animated: false,completion: nil)
override func viewDidLoad() { super.viewDidLoad() view.alpha =0}
覆盖函数viewWillAppear(动画: Bool) {super.viewWillAppear(动画) UIView.animateWithDuration(0.25) { self.view.alpha =1}}覆盖函数viewWillDisappear(动画: Bool) {super.viewWillDisappear(动画) UIView.animateWithDuration(0.25) { self.view.alpha =0} }
发布于 2016-10-12 20:41:41
唯一正确的方法是创建自定义模式转换和动画效果属性。请参阅https://stackoverflow.com/a/39709740
https://stackoverflow.com/questions/39644775
复制相似问题