iOS SDK详解之模糊(毛玻璃)效果效果

原创blog,转载请注明出处

http://blog.csdn.net/hello_hwc?viewmode=list



前言:

在iOS 8 之前,想要实现模糊效果,一般会使用一些Github库,当然自己定制也可以,其原理就是用Core Image进行一些数字图像处理(因为电子出身,本课的时候做过,用矩阵来做)。不过,到了iOS 8之后,这一切变的非常简单,因为Apple公开了之前的几个私有API。


Demo效果

三种Blur

Vibrancy(也就是在Blur上加一些想要强调的部分)


Demo下载链接

http://download.csdn.net/detail/hello_hwc/8678439


添加Blur

原理很简单

  • UIBlurEffect初始化一个blurEffect
  • 制定一个VisualEffectView,这个View定义了blur的区域
  • 把这个View作为Subview添加到想要blur的view上
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [bluredEffectView setFrame:  CGRectInset(self.imageview.bounds, 20, 20);
];
    bluredEffectView.layer.cornerRadius = 15;
    bluredEffectView.layer.masksToBounds = YES;
    [self.imageview addSubview:bluredEffectView];

其中Blur有三种,对应上文Demo图中的三种:

  • dark
  • light
  • extraLihgt

几点要注意的是

1. 不要对visualView设置alpha < 1

2. 可以对visualView设置Mask,来定制模糊的区域


添加Vibrancy

添加Vibrancy的原理就是在Blur的基础上再添加一个VisualView,并且在这个VisualView的contentView上添加想要的控件

   UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *bluredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [bluredEffectView setFrame:CGRectMake(30,self.imageview.bounds.size.height - 50,self.imageview.bounds.size.width - 60, 40)];
    bluredEffectView.layer.cornerRadius = 15;
    bluredEffectView.layer.masksToBounds = YES;
    [self.imageview addSubview:bluredEffectView];

    UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
    UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
    [vibrancyEffectView setFrame:self.imageview.bounds];
    [bluredEffectView.contentView addSubview:vibrancyEffectView];

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,self.imageview.bounds.size.width - 60, 40)];
    label.text = @"Highlight";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    [label setTextColor:[UIColor blackColor]];
    [vibrancyEffectView.contentView addSubview:label];

效果


简单介绍下我写的Demo的一些设计原理

由两个数组保存Model

-(NSArray *)blurEffectArray{
    return @[[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark],
             [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight],
             [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight],
             ];
}
-(NSArray *)titleArray{
    return @[@"Dark",@"Light",@"ExtraLight"];
}

由CurrentIndex来获取/设定当前选择效果,在CurrentIndex的set函数里进行Model和View的同步

-(void)setCurrentIndex:(NSInteger)currentIndex{
    if (self.visualview != nil) {
        [self.visualview removeFromSuperview];
    }
    self.visualview = [[UIVisualEffectView alloc] initWithEffect:[[self blurEffectArray] objectAtIndex:currentIndex]];
    self.visualview.frame = CGRectInset(self.imageview.bounds, 20, 20);
    self.visualview.layer.cornerRadius = 15;
    self.visualview.layer.masksToBounds = YES;
    self.navigationItem.title = [[self titleArray] objectAtIndex:currentIndex];
    [self.imageview addSubview:self.visualview];
    _currentIndex = currentIndex;

}

手势触摸,只需要改变CurrentIndex即可

- (IBAction)swipt:(UISwipeGestureRecognizer *)sender {
    self.currentIndex = (self.currentIndex + 1)%[self blurEffectArray].count;
}

初始化的时候,指定最初的Index

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageview.userInteractionEnabled = YES;
    self.currentIndex = 0;
    // Do any additional setup after loading the view, typically from a nib.
}

时间: 2024-08-03 22:28:40

iOS SDK详解之模糊(毛玻璃)效果效果的相关文章

iOS SDK详解之IBInspectable和IB_DESIGNABLE-Storyboad动态刷新

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS-SDK详解专栏,在这里你能找到很多iOS开发基础的文章 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言: 在做应用的UI设计的时候,如果属性能够在Interface Builder的图形化界面进行设置,并且动态的预览到效果,那样无疑会大大提高应用的开发效率.而XCode为我们提供了这样的一种方式,就是使用IBInspe

iOS SDK详解之NSScanner-分析String

原创blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK详解专栏,这里有很多基础的文章 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言:NSScanner是分析String,把String转为substring和数字的很好的工具.它使用一个NSString初始化,使用的时候通常从开头处扫描直到结尾. 本文会先举出两个例子,然后详细的讲解NSScanner的方法.源码是

iOS SDK详解之NSCoding协议

原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 欢迎关注我的iOS SDK详解专栏 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言:NSCoding是对iOS中的Model类进行编码和解码必须要遵循的协议,如果一个对象要被归档,那么这个协议是必须的. NSCoding要实现两个方法 - initWithCoder: //解码 - enc

IOS SDK详解

来源:http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html?page=1#42803301 博客专栏>移动开发专栏>IOS SDK详解 分享到:新浪微博腾讯微博IOS SDK详解 本专栏从IOS SDK中常用的Framework出发,继而深入的介绍各个Framework.每个Framework博主都会进行Demo 收藏 订阅 最新更新文章 [移动开发] IOS SDK详解之CALayer(二) 原创Blog,转载请注明出处

iOS SDK详解之NSCalendar &amp; NSDate?Components

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢饮关注我的iOS SDK详解专栏 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言:NSCalendar 和 NSDate?Components是有关iOS 时间相关API很重要的两个类.最近刚好用到,这里就整理下. 概念 NSCalendar 顾名思义就是日历,封装了系统如何按照年月日的方式来组织时间,组织时间的方式和地区,时区有很大关

IOS SDK详解之KVC

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:本文的架构 KVC的定义 KVC的几个场景 希望,通过本文让不了解KVC的同学入门,KVC在IOS开发中是个比较重要的概念,也是理解KVO的基础. 一 KVC的定义 KVC的全称是key-value coding,通过key-value的方式来访问属性.在很多地方,KVC是非常方便的. 属性 @property (strong,nonatomic) NSString * message; 赋值 [self set

IOS SDK详解之沙盒(一)图解+小工具

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言: IOS沙盒机制限制了App的访问权限,进而保护用户的数据信息. 一 查看沙盒结构 和一些百度来的博客显示隐藏稳文件的方式不同,本文也提供两种方式,简单粗暴. 方式一 使用工具simpholders(推荐) 下载链接 http://simpholders.com/ 效果如图 方式二 直接使用代码 用以下代码,log出documents/路径 NSURL * url = [[[NSFileManager defau

IOS SDK详解之拍照/相册(默认+自定义拍照界面)

原创blog,转载请注明出处 blog.csdn.net/hello_hwc 前言: 本来要更新NSURLSession的UploadTask的,结果写那个Demo的时候想要写成拍照上传,然后就想到先写一个关于拍照的Demo吧.本文会先介绍下如何使用系统提供的界面拍照和选择相册,然后自定义拍照界面.注意,本文使用的是UIImagePickerController,所以不能完全的自定义,如果想要彻底的自定义拍照,建议选择AV Foundation这个框架来做 Demo效果 进入系统的拍照界面 进入

iOS SDK详解之App使用Touch ID认证

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:从iOS 8.0之后,Apple 开放了App使用Touch ID来认证.但是有几点要注意: App只是把认证的过程代理给iOS系统了,并不能获得指纹的具体信息 不要试图把指纹信息和其他的数据库比较,在我写的这天还是不可能的. 使用的效果图 开始验证 验证失败 支持的设备 iPhone 5s + iPad Air 2 + iPad Mini 3 + 如何使用 导入使用的framework #import <Loc