UIVisualEffectView(自定义模糊视图)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIVisualEffectView(自定义模糊视图)相关的知识,希望对你有一定的参考价值。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加SCrollView,把图片添加上去便于观察
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"qiaodan"]];
self.scrollView.contentSize = imageView.image.size;
self.scrollView.bounces = NO;//回弹效果
[self.scrollView addSubview:imageView];
[self.view addSubview:self.scrollView];
//设置模糊图层
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
//设置模糊范围尺寸,自由设置 模糊的 范围
effectView.frame = CGRectMake(0, 100, 375, 300);
//添加到view
[self.view addSubview:effectView];
//可以在模糊图层上面添加内容:比如文字 等
//上面还可以添加模糊图层 子模糊图层
UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
label.text = @"乔丹";
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:50.f];
// [effectView.contentView addSubview:label];//1效果:添加到第一个模糊视图上面 ,字体部分会有 实体透明的效果
//设置子模糊视图
UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
subEffectView.frame = effectView.bounds;
[effectView.contentView addSubview:subEffectView];
[subEffectView.contentView addSubview:label];//2效果:这个字体 的部分会有 模糊的效果
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上是关于UIVisualEffectView(自定义模糊视图)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 8 中将 UIVisualEffectView 添加到 UINavigationBar?