I was wondering how to set the radius/blur factor of iOS new UIBlurEffectStyle.Light
? I could not find anything in the documentation. But I want it to look similar to the classic UIImage+ImageEffects.h
blur effect.
required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) let blur = UIBlurEffect(style: UIBlurEffectStyle.Light) let effectView = UIVisualEffectView(effect: blur) effectView.frame = frame addSubview(effectView) }
Changing alpha is not a perfect solution. It does not affect blur intensity. You can setup an animation from nil
to target blur effect and manually set time offset to get desired blur intensity. Unfortunately iOS will reset the animation offset when app returns from background.
Thankfully there is a simple solution that works on iOS >= 10. You can use UIViewPropertyAnimator
. I didn't notice any issues with using it. I keeps custom blur intensity when app returns from background. Here is how you can implement it:
class CustomIntensityVisualEffectView: UIVisualEffectView { /// Create visual effect view with given effect and its intensity /// /// - Parameters: /// - effect: visual effect, eg UIBlurEffect(style: .dark) /// - intensity: custom intensity from 0.0 (no effect) to 1.0 (full effect) using linear scale init(effect: UIVisualEffect, intensity: CGFloat) { super.init(effect: nil) animator = UIViewPropertyAnimator(duration: 1, curve: .linear) { [unowned self] in self.effect = effect } animator.fractionComplete = intensity } required init?(coder aDecoder: NSCoder) { fatalError() } // MARK: Private private var animator: UIViewPropertyAnimator! }
I also created a gist: https://gist.github.com/darrarski/29a2a4515508e385c90b3ffe6f975df7
You can change the alpha of the UIVisualEffectView that you add your blur effect to.
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.alpha = 0.5 blurEffectView.frame = self.view.bounds self.view.addSubview(blurEffectView)
This is not a true solution, as it doesn't actually change the radius of the blur, but I have found that it gets the job done with very little work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With