使用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 ...
随机推荐
- z-index随笔
z-index这个css属性是用来控制层级的,但是它的表现有点奇怪,这里做下记录. 假如有ABC三层,都没设置z-index时,是按dom节点顺序来控制层级. 此时如果B元素下有个元素D,设置了z-i ...
- Kerberos Ticket管理
Kerberos Ticket管理 本章介绍如何管理您的Kerberos Ticket,这里的Ticket是指Ticket-Granting-Ticket(TGT),是您访问集群中服务的凭证.我们假设 ...
- lua解析json
自己写的lua解析json,带容错,如果要是用于格式检查,得修改下.很简单直接贴代码 --------------------------------------------------json解析- ...
- Django redis2 列表 和其他操作
列表的操作 List操作,redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush插值至列表最左边 lpush(name,values) # 在name对应的list ...
- ZOJ - 3537 Cake (凸包+区间DP+最优三角剖分)
Description You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut t ...
- linux c编程:线程互斥一
当多个线程共享相同的内存的时候,需要确保每个线程都看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的.那么就不存在一致性问题.同样,如果变量是只读的,多个线程也不会有一致性的问题. ...
- bug-5——(js)indexOf()
indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置. ①对大小写敏感 ②如果要检索的字符串值没有出现,则该方法返回-1. ③位置时从0开始的 $j(this).html().ind ...
- Shell传参的多种方式
Shell 传参的多种方式 使用$1 $2 这种类似占位符的方式 # 命令行调用 start.sh 8080 9090 # 脚本中获取 port1=$1 # 8080 port2=$2 # 9090 ...
- PullToRefresh下拉刷新
https://github.com/chrisbanes/Android-PullToRefresh
- JSP页面EL表达式不解析
问题是这样:在搭建springMVC环境的时候,笔者写了一个简单的Controller如下: @Controller public class HelloController { @RequestMa ...