最新技术Swift:从入门到放弃实用工具

UIVisualEffectView实现高斯模糊效果(stroy

2016-08-04  本文已影响1595人  codeTao

在iOS 8后,新增了创建毛玻璃(blur)的接口.
通常要想创建一个特殊效果(如blur效果),可以创建一个UIVisualEffectView视图对象,这个对象提供了一种简单的方式来实现复杂的视觉效果。这个可以把这个对象看作是效果的一个容器,实际的效果会影响到该视图对象底下的内容,或者是添加到该视图对象的contentView中内容.

一.纯代码实现:

在当前视图控制器上添加了一个UIImageView作为背景图。然后在视图的一小部分中使用了blur效果

OC代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"a"];
[self.view addSubview:imageView];

// Blur effect 模糊效果
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = self.view.bounds;
[self.view addSubview:blurEffectView];

// Vibrancy effect 生动效果
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
vibrancyEffectView.frame = self.view.bounds;

// 效果字体
UILabel *label = [[UILabel alloc] init];
label.text = @"sk666";
label.font = [UIFont systemFontOfSize:72.0f];

[label sizeToFit];
label.center = self.view.center;

// 添加label到the vibrancy view的contentView上
[vibrancyEffectView.contentView addSubview:label];

// 添加vibrancy view 到 blur view的contentView上
[blurEffectView.contentView addSubview:vibrancyEffectView];


Swift代码:

let imageView = UIImageView(frame: view.bounds)
imageView.image = UIImage(named: "a")
view.addSubview(imageView)

// Blur Effect 模糊效果
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds

//添加到当前view上
view.addSubview(blurEffectView)

// Vibrancy Effect 生动效果
let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
vibrancyEffectView.frame = view.bounds

// 效果字体
let label = UILabel()
label.text = "sk666"
label.font = UIFont.systemFontOfSize(72.0)
label.sizeToFit()
label.center = view.center

//添加label到vibrancyView的contentView上
vibrancyEffectView.contentView.addSubview(label)

//添加vibrancyView到blurView的contentView上
blurEffectView.contentView.addSubview(vibrancyEffectView)


注意:

  1. 不要直接添加子视图到UIVisualEffectView视图中,而是应该添加到UIVisualEffectView对象的contentView中
  2. 尽量避免将UIVisualEffectView对象的alpha值设置为小于1.0的值,因为创建半透明的视图会导致系统在离屏渲染时去对UIVisualEffectView对象及所有的相关的子视图做混合操作。这不但消耗CPU/GPU,也可能会导致许多效果显示不正确或者根本不显示。

效果图:

a1.png

二.Stroyboar实现效果

Visual Effect Views with Blur

a4.png a3.png

注意:这里要展示的子控件imageView添加到UIVisualEffectView的contentView上

Visual Effect Views with Blur and Vibrancy

a5.png

效果图:


a7.png

操作:


a8.png

效果图:

a9.png

三.UIVisualEffectView构造

查看官方文档,可以看到在UIVisualEffectView.h中,定义了3个专门用来创建视觉特效的类,它们分别是UIVisualEffectUIBlurEffectUIVibrancyEffect

继承关系:
UIVisualEffect 继承自 NSObject.

UIVisualEffect 有两个子类:UIBlurEffectUIVibrancyEffect

typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {
    UIBlurEffectStyleExtraLight, //额外亮
    UIBlurEffectStyleLight, // 亮
    UIBlurEffectStyleDark // 暗
} NS_ENUM_AVAILABLE_IOS(8_0);

vibrancy特效是取决于颜色值的。所有添加到contentView的子视图都必须实现tintColorDidChange方法并更新自己。

需要注意的是,我们使用
UIVibrancyEffect(forBlurEffect:) //Swift
或者
+ (UIVibrancyEffect *)effectForBlurEffect:(UIBlurEffect *)blurEffect; //OC
方法创建UIVibrancyEffect时,
参数blurEffect必须是我们想加效果的那个blurEffect,否则可能不是我们想要的效果。
上一篇 下一篇

猜你喜欢

热点阅读