iOS_UIVisualEffectView (毛玻璃效果)
说明:
毛玻璃的使用虽然很简单, 但我还是在这片博文中引用了大量的苹果官方API. 我希望任何浏览过这片博文的人不仅可以知道如何使用UIVisualEffectView这个类, 也可以知道它的原理. 其实任何一个类都可以在苹果官方API中找到它的简单使用方法, 也希望大家能够掌握这种学习方法.希望大家能够耐心的, 按照顺序的看完这片博文.
文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)
- 此文章由 @春雨 编写. 经 @Scott,@黑子 审核. 若转载此文章,请注明出处和作者
UIVisualEffectView
核心API
Class : UIVisualEffectView, UIVisualEffect, UIBlurEffect, UIVibrancyEffect
涉及的API:(API的官方详细注释详见本章结尾)
/** UIVisualEffectView的初始化方法. */
- (instancetype)initWithEffect:(UIVisualEffect *)effect
/** UIBlurEffect的类方法. */
+ (UIBlurEffect *)effectWithStyle:(UIBlurEffectStyle)style
/** UIVibrancyEffect的类方法. */
+ (UIVibrancyEffect *)effectForBlurEffect:(UIBlurEffect *)blurEffect
功能实现
思路:
1 . 实现UIBlurEffect类型的毛玻璃效果
- 创建一个imageView和一个lable.
- 创建blur类型的UIVisualView
- 最后实现毛玻璃效果
2 . 实现UIVibrancyEffect类型的毛玻璃效果
- 创建一个imageView和一个lable.
- 创建vibrancy类型的UIVisualView
- 最后实现毛玻璃效果
3 . 用于消息通知中的毛玻璃效果.
code
1 . 实现UIBlurEffect类型的毛玻璃效果
/** 1. 创建UIImageView的对象blurImageView. */
UIImageView *blurImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 2 * self.view.frame.size.width, self.view.frame.size.height)];
UIImage *image = [UIImage imageNamed:@"a.jpg"];
blurImageView.image = image;
[self.view addSubview:blurImageView];
[blurImageView release];
/** 2. 创建UILable的对象. */
UILabel *blurLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, self.view.frame.size.width - 60, 300)];
blurLabel.text = @"Our mind is sponge, our heart is stream.";
/** 设置blurLabel的最大行数. */
blurLabel.numberOfLines = 2;
/** 设置blurLabel的字体颜色. */
blurLabel.textColor = [UIColor whiteColor];
/** 设置blurLabel的字体为系统粗体, 字体大小为34. */
blurLabel.font = [UIFont boldSystemFontOfSize:34