2

How can i reduce the blur effect on a UIVisualEffectView it gives me options of light, extraLight and dark which are not good enough for me i am trying to achieve something like this enter image description here

Sagaya Abdul
  • 151
  • 1
  • 11
  • Possible duplicate of [Less Blur with \`Visual Effect View with Blur\`?](https://stackoverflow.com/questions/29498884/less-blur-with-visual-effect-view-with-blur) – Tamás Sengel Mar 12 '18 at 15:27
  • 1
    If what's explained in the dup link doesn't help, consider using CoreImage. Not only do you have more control over a blur effect, you have eight different ones. –  Mar 12 '18 at 15:35
  • check this post https://stackoverflow.com/questions/41156542/how-to-blur-an-existing-image-in-a-uiimageview-with-swift/41157042#41157042 – Joe Mar 12 '18 at 16:43

4 Answers4

5

We can do that absolutely natively and with correct expected look using animator

Usage:

let blurEffectView = BlurEffectView()
view.addSubview(blurEffectView)

BlurEffectView realisation:

class BlurEffectView: UIVisualEffectView {
    
    var animator = UIViewPropertyAnimator(duration: 1, curve: .linear)
    
    override func didMoveToSuperview() {
        guard let superview = superview else { return }
        backgroundColor = .clear
        frame = superview.bounds //Or setup constraints instead
        setupBlur()
    }
    
    private func setupBlur() {
        animator.stopAnimation(true)
        effect = nil

        animator.addAnimations { [weak self] in
            self?.effect = UIBlurEffect(style: .dark)
        }
        animator.fractionComplete = 0.1   //This is your blur intensity in range 0 - 1
    }
    
    deinit {
        animator.stopAnimation(true)
    }
}
bodich
  • 1,708
  • 12
  • 31
1

Here's my riff on bodich's answer that fixes the problem of the blur resetting each time the app comes to the foreground. I'm still not convinced this hack is shippable since there may be other times that CoreAnimation decides to finish stale animations in which case the blur would suddenly become full intensity.

class BlurEffectView: UIVisualEffectView {
    
    private var animator = UIViewPropertyAnimator(duration: 1, curve: .linear)
    private var intensity: CGFloat = 0.25
    
    init(intensity: CGFloat) {
        self.intensity = intensity
        super.init(effect: nil)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    deinit {
        animator.stopAnimation(true)
    }

    override func didMoveToSuperview() {
        guard let superview = superview else { return }

        backgroundColor = .clear
        frame = superview.bounds
        autoresizingMask = [.flexibleWidth, .flexibleHeight]
        clipsToBounds = true
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(appWillEnterFG(_:)),
            name:UIApplication.willEnterForegroundNotification,
            object: nil
        )

        setUpAnimation()
    }
    
    private func setUpAnimation() {
        animator.stopAnimation(true)
        effect = nil

        animator.addAnimations { [weak self] in
            self?.effect = UIBlurEffect(style: .light)
        }
        animator.fractionComplete = intensity
    }
    
    @objc func appWillEnterFG(_ note: Notification) {
        setUpAnimation()
    }
}
John Scalo
  • 3,194
  • 1
  • 27
  • 35
0

There is no official way to change the blur level.

However, you can try something like this:

Create a custom blur view

-1

I know this is late, but if you're just using blur for a modal presentations, apple has a built-in UIModalPresentationStyle called blurOverFullScreen, and it works pretty well.

Just set the parent controller's modalPresentationStyle to .blurOverFullScreen and present the new view controller. If the new view is smaller than the screen, the background should be blurred out like seen in your picture.

Ethan Zhao
  • 229
  • 1
  • 6
  • 18