频道栏目
首页 > 资讯 > IOS > 正文

iOS - 毛玻璃效果

16-05-09        来源:[db:作者]  
收藏   我要投稿

iOS8之后苹果提供了制作毛玻璃效果的API

就是这个UIVisualEffectView,用这个initWithEffect:去初始化,然后呢,他有两种效果继承自UIVisualEffect。这个父类不用管,什么也不做,主要看他的两个子类UIBlurEffect和UIVibrancyEffect。

UIBlurEffect : 这个是影响毛玻璃后面视图的

效果图:

 

这里写图片描述

 

UIVibrancyEffect : 这个是影响毛玻璃上的视图的

 

这里写图片描述

 

是不是很漂亮,做起来也不难呢。

先说毛玻璃下面的效果的做法。

你先初始化一个UIBlurEffect对象,他有三种风格

typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {

UIBlurEffectStyleExtraLight,

UIBlurEffectStyleLight,

UIBlurEffectStyleDark

}

上面的效果都是用UIBlurEffectStyleLight做出来的

UIBlurEffectStyleExtraLight效果如下

 

这里写图片描述

 

UIBlurEffectStyleDark效果如下

 

这里写图片描述

 

然后呢用这个UIBlurEffect对象创建一个UIVisualEffectView对象。用你需要被虚化的视图添加这个UIVisualEffectView对象为子视图就可以了。

接着说毛玻璃上面的效果

创建一个UIVibrancyEffect对象,用之前创建的blur去初始化,然后创建一个UIVisualEffectView对象,用这个UIVibrancyEffect对象初始化,最后将你想要添加的子视图添加到UIVisualEffectView的contentView上就可以了。

完整代码如下:

// UIBlurEffect效果
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
imageView.image = [UIImage imageNamed:@"pic"];
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *ev = [[UIVisualEffectView alloc] initWithEffect:blur];
ev.frame = self.view.frame;
[imageView addSubview:ev];

// UIVibrancyEffect效果
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blur];
UIVisualEffectView *ano = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
ano.frame = self.view.frame;

UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:40];
label.frame = CGRectMake(100, 100, 400, 100);
label.text = @"Beautiful View";
[ev.contentView addSubview:ano];
[ano.contentView addSubview:label];

[self.view addSubview:imageView];
相关TAG标签
上一篇:iOS开发系列--无限循环的图片浏览器
下一篇:OCiOS动效设计:UITableView 实现滚动视差效果
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站