使用UIVisualEffectView创建毛玻璃效果
UIVisuaEffectView :继承自UIView,可以看成是专门用于处理毛玻璃效果的视图,只要我们将这个特殊的View添加到其他视图(eg. ImageView )上面,被该UIVisuaEffectView遮盖的部分看起来就有了毛玻璃效果。使用UIVisuaEffectView有一点需要特别注意,不要在UIVisuaEffectView实例化View上面直接添加subViews,应该将需要添加的子视图添加到其contentView上。同时,尽量避免将UIVisualEffectView对象的alpha值设置为小于1.0的值,因为创建半透明的视图会导致系统在离屏渲染时去对UIVisualEffectView对象及所有的相关的子视图做混合操作,比较消耗性能。
效果截图:

实现代码:
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIVisualEffect *effect; @end
ViewController.m
#import "ViewController.h"
static int blurTag = ;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//添加ImageView
self.imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.imageView setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"" ofType:@"png"]]];
[self.view addSubview:self.imageView];
//显示所有可用的模糊效果
CGFloat startX = ;
CGFloat startY = ;
CGFloat startW = ;
CGFloat startH = ;
{//UIBlurEffectStyleExtraLight
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
[effectView setFrame:CGRectMake(startX, startY, startW, startH)];
effectView.tag = blurTag;
[self.view addSubview:effectView];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
effectView.userInteractionEnabled = YES;
[effectView addGestureRecognizer:gesture];
}
{//UIBlurEffectStyleLight
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
[effectView setFrame:CGRectMake(startX + (startW + ), startY, startW, startH)];
effectView.tag = blurTag + ;
[self.view addSubview:effectView];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
effectView.userInteractionEnabled = YES;
[effectView addGestureRecognizer:gesture];
}
{//UIBlurEffectStyleDark
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
[effectView setFrame:CGRectMake(startX + (startW + )*, startY, startW, startH)];
effectView.tag = blurTag + ;
[self.view addSubview:effectView];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
effectView.userInteractionEnabled = YES;
[effectView addGestureRecognizer:gesture];
}
// {//UIBlurEffectStyleExtraDark
// UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraDark];
// UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
// [effectView setFrame:CGRectMake(startX + (startW + 5)*3, startY, startW, startH)];
// [self.view addSubview:effectView];
// }
{//UIBlurEffectStyleRegular
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
[effectView setFrame:CGRectMake(startX + (startW + )*, startY, startW, startH)];
effectView.tag = blurTag + ;
[self.view addSubview:effectView];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
effectView.userInteractionEnabled = YES;
[effectView addGestureRecognizer:gesture];
}
{//UIBlurEffectStyleProminent
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleProminent];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
[effectView setFrame:CGRectMake(startX + (startW + )*, startY, startW, startH)];
effectView.tag = blurTag + ;
[self.view addSubview:effectView];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
effectView.userInteractionEnabled = YES;
[effectView addGestureRecognizer:gesture];
}
//添加UIVisualEffectView
self.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight ];
self.effectView = [[UIVisualEffectView alloc] initWithEffect:self.effect];
self.effectView.layer.cornerRadius = ;
self.effectView.layer.masksToBounds = YES;
[self.effectView setFrame:CGRectMake(, , , )];
[self.view addSubview:self.effectView];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[btn setTitle:@"标题" forState:UIControlStateNormal];
[self.effectView.contentView addSubview:btn];
}
- (void)tapAction:(UITapGestureRecognizer *)gesture{
NSInteger tag = gesture.view.tag - blurTag;
switch (tag) {
case :
[self.effectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
break;
case :
[self.effectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
break;
case :
[self.effectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
break;
case :
[self.effectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular]];
break;
case :
[self.effectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleProminent]];
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
参考链接:
http://www.jianshu.com/p/d115836ed3fa
使用UIVisualEffectView创建毛玻璃效果的更多相关文章
- Swift 之模糊效果(毛玻璃效果,虚化效果)的实现
前言: 之前项目中有用到过Objective-C的的模糊效果,感觉很是不错,而且iOS8之后官方SDK也直接提供了可以实现毛玻璃效果的三个类:UIBlurEffect.UIVibrancyEffect ...
- iOS 实现毛玻璃效果
话说苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如下图的通知中心界面; 但是其iOS7.0的SDK并没有提供给开发者实现毛玻璃效果的API,所以很多人都是通过一些别人 ...
- iOS 实现简单的毛玻璃效果
最近在整理导航栏的渐隐渐现效果,整理过程中偶然学会了图片的毛玻璃效果实现,很简单,不多说了,先上图看看效果对比, 这是原图, 这是加了效果后的,创建图片的代码就不上了,下面看下添加效果的代码: // ...
- iOS开发探索-高斯模糊&毛玻璃效果
iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效 ...
- iOS 毛玻璃效果的实现方法
iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效 ...
- iOS模糊效果(毛玻璃效果)的实现
前一段时间项目中用到毛玻璃效果,那时对UIBlurEffect类和 UIVisualEffectView这两个类做了一部分了解.但当时并没有去特别的深入研究,直到项目做完后,才静下心来好好研究了一番. ...
- iOS开发小技巧--实现毛玻璃效果的方法
一.美工出图 二.第三方框架 -- DRNRealTimeBlur,框架继承自UIView.使用方法:创建UIView直接继承自框架的View,就有了毛玻璃效果 三.CoreImage -- 图片加高 ...
- iOS_自定义毛玻璃效果
http://www.2cto.com/kf/201408/329969.html 最终效果图: 关键代码: UIImage分类代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...
- iOS_自己定义毛玻璃效果
终于效果图: 关键代码: UIImage分类代码 // // UIImage+BlurGlass.h // 帅哥_团购 // // Created by beyond on 14-8-30. // C ...
随机推荐
- 洛谷P1073 最优贸易==codevs1173 最优贸易
P1073 最优贸易 题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一 ...
- Nearest Common Ancestors(LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- ibatis实现Iterate的使用 (转)
<iterate property="" /*可选, 从传入的参数集合中使用属性名去获取值, 这个必须是一 ...
- tomcat日志按天切分
1. 下载工具cronolog wget http://cronolog.org/download/cronolog-1.6.2.tar.gz 这是网上流传的下载地址,好像没用,所以需要自己去网上找. ...
- Netbeans8.0设置Consola字体并解决中文乱码问题
在Netbeans8.0上开发php,设置字体为Consola后.发现中文显示是乱码的.经过改动jre的配置文件成功攻克了这个问题. 1. 进入jdk安装文件夹下/jre/lib文件夹,找到fontc ...
- python+NLTK 自然语言学习处理五:词典资源
前面介绍了很多NLTK中携带的词典资源,这些词典资源对于我们处理文本是有大的作用的,比如实现这样一个功能,寻找由egivronl几个字母组成的单词.且组成的单词每个字母的次数不得超过egivronl中 ...
- 常见数据挖掘算法的Map-Reduce策略(1)
大数据这个名词是被炒得越来越火了,各种大数据技术层出不穷,做数据挖掘的也跟着火了一把,呵呵,现今机器学习算法常见的并行实现方式:MPI,Map-Reduce计算框架,GPU方面,grap ...
- (转载)DataTable与List<T>相互转换
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- LeetCode:字母异位词分组【16】
LeetCode:字母异位词分组[16] 题目描述 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", &quo ...
- LeetCode:救生艇【881】
LeetCode:救生艇[881] 题目描述 第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. ...