Swift使用BlurView进行高斯模糊操作(毛玻璃)

使用BlurView进行高斯模糊操作:

使用步骤:
创建一个UIImageView来进行高斯模糊的底层显示(要毛玻璃效果肯定要有背景),然后创建UIBlurEffect,这是控制高斯模糊的显示样式的,接着创建UIVisualEffectView(其构造方法内传值即为UIBlurEffect对象),最后将UIVisualEffectView添加到UIImageView上面即可

import UIKit

class BlurViewController: UIViewController {
    
    var imageView = UIImageView()
    
    func initView(){
        // 设置高斯模糊效果
        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.height)
        blurView.layer.masksToBounds = true
        
        let imageObject = UIImage(named: "kicinio")
        imageView.image = imageObject
        imageView.contentMode = .scaleAspectFill
        self.view.addSubview(imageView)

        // 将UIVisualEffectView添加到ImageView上面即可
        self.imageView.addSubview(blurView)
        
        imageView.snp.makeConstraints{ make in
            make.width.equalToSuperview()
            make.top.equalToSuperview()
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
     
        initView()
        
    }

}

效果:
原图:(梵高.向日葵,[1888.8.8])
在这里插入图片描述

高斯模糊后:
在这里插入图片描述

在BlurView上面显示文字:

步骤:创建UIVibrancyEffect的UIVisualEffectView类,然后添加到UIBlurView上,接着将UILabel添加到一开始的UIVisualEffectView类上即可。

import UIKit

class BlurViewController: UIViewController {
    
    var imageView = UIImageView()
    
    func initView(){
        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.height)
        blurView.layer.masksToBounds = true
        
        let vibraView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect))
        vibraView.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.height)
        blurView.contentView.addSubview(vibraView)
        
        let labelOne = UILabel(frame: CGRect(x: (self.view.frame.width / 2) - (100), y: (self.view.frame.height / 3) , width: 200, height:30))
        labelOne.text = "For Kicinio"
        labelOne.font = UIFont.boldSystemFont(ofSize: 40)
        vibraView.contentView.addSubview(labelOne)
        
        let imageObject = UIImage(named: "flower")
        imageView.image = imageObject
        imageView.contentMode = .scaleAspectFill
        self.view.addSubview(imageView)
        self.imageView.addSubview(blurView)
        
        imageView.snp.makeConstraints{ make in
            make.width.equalToSuperview()
            make.top.equalToSuperview()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()      
        
        initView()
        
    }

}

效果图:
在这里插入图片描述

使用圆角显示:

添加如下语句到UIBlurView的属性即可:

blurView.layer.cornerRadius = 25

数值即为圆角半径,越大圆角越大。
效果图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值