Less Blur with `Visual Effect View with Blur`?

后端 未结 11 1323
死守一世寂寞
死守一世寂寞 2020-12-08 10:11

Title pretty much asks it all...

I\'m playing with the iOS8 Visual Effect View with Blur. It\'s over a UIImageView that shows a user choosa

11条回答
  •  不知归路
    2020-12-08 10:59

    You can add a UIBlurEffect over the image. And that will do the trick.

    Here is an example of a UIImageView with blur effect on it. Remember to add a Image to the UIImageView.

    Adjust blur amount with blurEffectView.alpha = 0.8 (from 0 to 1)

    import UIKit
    
    class BlurEffectImageView: UIImageView {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        addBlurEffect()
    }
    
    private func addBlurEffect(){
        let blurEffect = UIBlurEffect(style: .light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.alpha = 0.8
    
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
        blurEffectView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(blurEffectView)
    
        NSLayoutConstraint(item: blurEffectView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: blurEffectView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: blurEffectView, attribute: .height,  relatedBy: .equal, toItem: self, attribute: .height,  multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: blurEffectView, attribute: .width,   relatedBy: .equal, toItem: self, attribute: .width,   multiplier: 1.0, constant: 0).isActive = true
      }
    
    }
    

提交回复
热议问题