swift4.0-为视图添加模糊效果
2018-03-14 本文已影响62人
落夏简叶
UIVisualEffect两个子类UIBlurEffect和UIVibrancyEffect
UIBlurEffect表模糊的一个类,传入UIBlurEffectStyle类型的值来决定创建怎么样的模糊效果
UIVibrancyEffect会显示后面涂层的颜色
UIBlurEffectStyle是一个枚举类型
public enum UIBlurEffectStyle : Int {
case extraLight
case light
case dark
}
UIVisualEffectView 用来接收UIVisualEffect类型的视觉效果
使用UIBlurEffect创建的模糊效果
UIBlurEffect@2x.png代码如下
func blurEffct() -> Void {
let visualEffect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: visualEffect)
blurView.alpha = 0.5
blurView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
imgView.addSubview(blurView)
}
使用UIVibrancyEffect创建的模糊效果
UIVibrancyEffect@2x.png代码如下:
func vibrancyEffct() -> Void {
let txtLable = UILabel()
txtLable.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
txtLable.text = "shine blur something"
txtLable.textColor = UIColor.red //无效
txtLable.tintColor = UIColor.red
txtLable.font = UIFont.boldSystemFont(ofSize: 20)
let visualEffect = UIBlurEffect(style: .extraLight)
let vibrancyEffct = UIVibrancyEffect(blurEffect: visualEffect)
let blurView = UIVisualEffectView(effect: vibrancyEffct)
blurView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
blurView.alpha = 0.5
blurView.contentView.addSubview(txtLable) //必须添加到contentView
imgView.addSubview(blurView)
}
UIBlurEffect直接对imgView视图进行模糊,vibrancyEffct是对添加到imgView上的txtLable视图进行模糊