使用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 ...
随机推荐
- Python 字符串拼接、格式化输出、深浅复制
拼接:"+"号(同类型可拼接) >>>li = [1,2] >>>li + li [1,2,1,2] >>>li*2 [1,2 ...
- ubuntu 安装wine
笔记 1.安装源 sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update 2.安装wine sudo apt-get inst ...
- 2.二级接口ListableBeanFactory
这个随笔主要讲的是ListableBeanFactory package org.springframework.beans.factory; import java.lang.annotation. ...
- 【译】Stackoverflow:Java Servlet 工作原理问答
导读 本文来自stackoverflow的问答,讨论了Java Servlet的工作机制,如何进行实例化.共享变量和多线程处理. 问题:Servlet 是如何工作的?Servlet 如何实例化.共享变 ...
- 简易bootloader重定位问题
单板选择NandFlash启动,则硬件上电后,系统会自己主动将NandFlash中的前4K内容复制到STEPSTONE即4K SRAM中.然后从SRAM中的0X0地址启动. 基于mini ...
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例(转载)
前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域 ...
- Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)
[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...
- 关于date和String互相转换的问题
其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleD ...
- 第2条:遵循PEP8风格指南
<Python Enhancement Proposal #8>(8号Python增强提案)又叫PEP8,它是针对Python代码格式而编订的风格指南. 尽管可以在保证语法正确的前提下随意 ...
- 用cocos2d-html5做的消除类游戏《英雄爱消除》(1)——系统主菜单
系统主菜单如下图所示: 首先,介绍下这个主菜单,它包含了一个动画logo以及一个按钮选项,动画logo每隔1秒钟切换一张图片,点击相应的按钮选项会切换不同的游戏场景. 下面看下这个界面的源码: /** ...