ios control blur effect fuzziness

Inherit UIVisualEffectView and use UIViewPropertyAnimator to control blur

class CustomVisualEffectView: 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!

}

The usage is as follows, I think 0.2 blur is more appropriate here.

let blurEffect = UIBlurEffect(style: .light)
let effectView = CustomVisualEffectView(effect: blurEffect, intensity: 0.2)
view.addSubview(effectView)

insert image description here
insert image description here

Note that
the transparency cannot be changed.
CustomVisualEffectView cannot place subviews on it (put it on other view layers)
and cannot be reused (that is, on UITableView, etc., only the blur layer can be recreated each time, and cannot be reused directly)

Guess you like

Origin blog.csdn.net/htwhtw123/article/details/130425345