IOS development sometimes involves blur effect processing, UIKit native has a UIVisualEffectView to support blur effect processing, but designers sometimes feel that the native blur effect is too heavy, and want to make it lighter, how to deal with that? Of course, you can choose some third party or implement your own obfuscation of the view, but what if you can’t do that? You can do this with a UIViewPropertyAnimator.

// The following code can be run directly on 'Playground' to see the effect
class BlurDemoViewController: UIViewController {
    let imageView = UIImageView(a)let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    
    var animator: UIViewPropertyAnimator?
    
    override func viewDidLoad(a) {
        super.viewDidLoad()
        
        imageView.frame = view.bounds
        imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        imageView.contentMode = .scaleAspectFill
        view.addSubview(imageView)
        
        effectView.frame = view.bounds
        effectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(effectView)
        
	    Duration is not important here because we will manually set the completion of the Animator
        animator = UIViewPropertyAnimator(duration: 1, curve: .linear) {
            // This is the core, so that controlling fractionComplete controls the transition between the view's default blur level and no blur effect
            self.effectView.effect = nil
        }
        
        // Slider is used to control the blur effect level demo
        let slider = UISlider()
        slider.frame.origin.x = 20
        slider.frame.origin.y = view.frame.size.height - slider.frame.size.height - 20
        slider.frame.size.width = view.frame.size.width - 40
        slider.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
        slider.addTarget(self, action: #selector(sliderValueChanged), for: .valueChanged)
        view.addSubview(slider)
    }
    
    @objc private func sliderValueChanged(sender: UISlider) {
        // Control the completion of the animator to achieve Blur level controlanimator? .fractionComplete =CGFloat(sender.value)
    }
}

let vc = BlurDemoViewController()
vc.imageView.image = UIImage(named: "Test")

//vc.effectView.effect = UIBlurEffect(style: .dark)
//vc.effectView.effect = UIBlurEffect(style: .extraLight)

import PlaygroundSupport
PlaygroundPage.current.liveView = vc

Copy the code

The same idea can be applied to other view properties.