实现毛玻璃效果主要涉及到三个类:UIBlurEffect、UIVibrancyEffect、UIVisualEffectView.
UIBlurEffect设置模糊情况的,而且系统提供了三种样式:
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark
代码实现:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//添加背景图
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]];
//毛玻璃效果1,系统的
//UIBlurEffect主要设置模糊样式
UIBlurEffect * blurEffect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView * blureffectView=[[UIVisualEffectView alloc]initWithEffect:blurEffect];
blureffectView.frame=CGRectMake(10, 74, 300, 300);
[self.view addSubview:blureffectView];
UIVibrancyEffect * vibrancyEffect=[UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView * vibrancyEffectView=[[UIVisualEffectView alloc]initWithEffect:vibrancyEffect];
vibrancyEffectView.frame=CGRectMake(10, 74, 300, 300);
UILabel * lable=[[UILabel alloc]initWithFrame:CGRectMake(100, 125, 100, 50)];
lable.text=@"哎呀呀";
lable.font=[UIFont systemFontOfSize:18];
lable.textColor=[UIColor magentaColor];//此时这里的颜色设置是没有用的,因为受到了UIVibrancyEffect设置的影响
//使lable上的显示内容会受到vibrancyEffectView颜色调节效果的影响
[vibrancyEffectView.contentView addSubview:lable];
[blureffectView.contentView addSubview:vibrancyEffectView];
//毛玻璃效果2,系统的
UILabel * lable2=[[UILabel alloc]initWithFrame:CGRectMake(100, 125, 100, 50)];
lable2.text=@"哎呀呀";
lable2.font=[UIFont systemFontOfSize:18];
lable2.textColor=[UIColor magentaColor];//因为没有设置UIVibrancyEffect,所以此时的颜色设置是有效的
UIBlurEffect * blurEffect2=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
UIVisualEffectView * blureffectView2=[[UIVisualEffectView alloc]initWithEffect:blurEffect2];
blureffectView2.frame=CGRectMake(100, 400, 260, 260);
[self.view addSubview:blureffectView2];
[blureffectView2.contentView addSubview:lable2];
}