Swift 之模糊效果(毛玻璃效果,虚化效果)的实现

时间:2023-03-08 16:52:29

前言:

之前项目中有用到过Objective-C的的模糊效果,感觉很是不错,而且iOS8之后官方SDK也直接提供了可以实现毛玻璃效果的三个类:UIBlurEffect、UIVibrancyEffect 和 UIVisualEffectView。通过这三个类可以很容易的去实现毛玻璃效果。

最近在熟悉Swift,所以今天说的是使用Swift去实现这种效果。

前提:

我们去实现的模糊效果有个前提,就是需要背景有一个色调,这样才能使毛玻璃有一个明显的效果,这里我直接在底部添加一个背景图片吧

Swift 之模糊效果(毛玻璃效果,虚化效果)的实现

我们就在这张背景图片上去实现模糊效果。

 //MARK: ******* 创建背景图片 *************
func createBackgroundImageView() {
self.imageView1 = UIImageView(frame: self.view.bounds) imageView1.layer.borderColor = UIColor.black.cgColor
imageView1.layer.borderWidth = imageView1.layer.cornerRadius = imageView1.clipsToBounds = true imageView1.image = UIImage(named: "") self.view.addSubview(imageView1) }

1、UIBlurEffect类(用作创建一个模糊效果)

SDK中提供的模糊效果有5种,其中两种是iOS10之后添加的,我们来看一下:

 /**
public enum UIBlurEffectStyle : Int { case extraLight case light case dark @available(iOS 10.0, *)
case regular // Adapts to user interface style @available(iOS 10.0, *)
case prominent // Adapts to user interface style
}
*/

我们就直接创建好了,代码如下:

//创建一个模糊效果
let blurEffect = UIBlurEffect(style: .light)

上面使用的是普通的亮色。

2、UIVibrancyEffect类

UIVibrancyEffect 主要用于放大和调整 UIVisualEffectView 视图下面的内容的颜色,同时让 UIVisualEffectView 的 contentView 中的内容看起来更加生动。

 //创建并添加vibrancy视图
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)

3、UIVisualEffectView类

UIVisualEffectView:模糊视图。将前面创建的 UIBlurEffect 类的实例应用到这个模糊视图上。将 UIVisualEffectView 类的实例(模糊视图)置于待毛玻璃化的视图之上即可。在其下方的所有视图都会有模糊效果。值得一提的是:不要直接添加子视图去UIVisualEffectView,而是要添加到contentView上。

//创建一个承载模糊效果的视图
let blurView = UIVisualEffectView(effect: blurEffect)

4、整体的代码实现

下面我们直接看整体的代码实现:

这部分分为两种,效果也是不一样的:

先看一下效果图:

Swift 之模糊效果(毛玻璃效果,虚化效果)的实现

4.1   UIBlurEffect和 UIVisualEffectView两者配合使用

    func showBlurEffect() {

        //创建一个模糊效果
let blurEffect = UIBlurEffect(style: .light)
//创建一个承载模糊效果的视图
let blurView = UIVisualEffectView(effect: blurEffect) blurView.frame = CGRect(x: , y: , width: viewWidth+*space, height: Double(viewHeight)) let label = UILabel(frame: CGRect(x: , y: , width: viewWidth - , height: )) label.text = "bfjnecsjdkcmslc,samosacmsacdfvneaui" label.font = UIFont.boldSystemFont(ofSize: )
label.numberOfLines =
label.textAlignment = .center
label.textColor = UIColor.white blurView.contentView.addSubview(label) self.view.addSubview(blurView) }

4.2  UIBlurEffect、UIVibrancyEffect 和 UIVisualEffectView

    func showBlurEffectWithUIVibrancyEffect() {
//创建一个模糊效果
let blurEffect = UIBlurEffect(style: .light)
//创建一个承载模糊效果的视图
let blurView = UIVisualEffectView(effect: blurEffect) blurView.frame = CGRect(x: , y: Double(viewHeight+), width: viewWidth+*space, height: Double(viewHeight)) self.view.addSubview(blurView) //创建并添加vibrancy视图
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect) let vibrancyView = UIVisualEffectView(effect:vibrancyEffect)
vibrancyView.frame = self.view.bounds let label = UILabel(frame: CGRect(x: , y: viewY, width: viewWidth - , height: )) label.text = "bfjnecsjdkcmslc,samosacmsacdfvneaui" label.font = UIFont.boldSystemFont(ofSize: )
label.numberOfLines =
label.textAlignment = .center
label.textColor = UIColor.white vibrancyView.contentView.addSubview(label) blurView.contentView.addSubview(vibrancyView) self.view.addSubview(blurView) }

图片的前者是:UIBlurEffect和 UIVisualEffectView两者配合使用

后者:UIBlurEffect、UIVibrancyEffect 和 UIVisualEffectView