You need to create a new UIViewControllerAnimatedTransitioning
.
Then in animateTransition(using transitionContext: UIViewControllerContextTransitioning)
you need to code your Animation.
Now in iOS 10 you can use UIViewPropertyAnimator
in order to animate the BlurRadius
of an UIVisualBlurEffect
.
Result :
Here you have an example of usage : https://github.com/PierrePerrin/PPBlurModalPresentation
First
You need to create you blur transition
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()
})
}
}
Then
You need to set the transitioning delegation in your 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
}
}