一.UIToolBar
iOS7 及之前的版本可以使用UIToolBar 快速定義自己的毛玻璃效果
UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"register_bg.jpeg"]];
imageView.frame =self.view.frame;
[self.view addSubview:imageView];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0, imageView.frame.size.width/2, imageView.frame.size.height)];
toolbar.barStyle = UIBarStyleBlackOpaque;
[imageView addSubview:toolbar];
UIToolBar.png
二.UIVisualEffectView類
在iOS8.0之后,蘋果新增了一個類UIVisualEffectView,可以快速實現毛玻璃效果。官方文檔也提供了幾個類的說明
1、UIBlurEffect: 創建模糊效果,也就是毛玻璃效果的類,可以設置風格。
2、UIVibrancyEffect: 作用是放大和調整UIVisualEffectView內容視圖的內容的顏色,讓UIVisualEffectView的contentView中的內容看起來更加生動
3、UIVisualEffect: 沒有任何方法,只是充當一個幫助UIVisualEffectView創建的對象,是UIBlurEffect和UIVibrancyEffect的父類
4、UIVisualEffectView:展示復雜的圖像效果。
通常UIVibrancyEffect對象是與UIBlurEffect一起使用,主要用于處理在UIBlurEffect特效上的一些顯示效果。
注意:
1、不要直接添加子視圖到UIVisualEffectView視圖中,而是應該添加到UIVisualEffectView對象的contentView中
2、盡量避免將UIVisualEffectView對象的alpha值設置為小于1.0的值,因為創建半透明的視圖會導致系統在離屏渲染時去對UIVisualEffectView對象及所有的相關的子視圖做混合操作。這不但消耗CPU/GPU,也可能會導致許多效果顯示不正確或者根本不顯示。
UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"register_bg.jpeg"]];
imageView.frame =self.view.frame;
[self.view addSubview:imageView];
//iOS 8.0
* * 模糊效果的三種風格
*
* @param UIBlurEffectStyle
*
* UIBlurEffectStyleExtraLight, //高亮
* UIBlurEffectStyleLight, //亮
* UIBlurEffectStyleDark //暗
* *
UIBlurEffect *blurEffect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView =[[UIVisualEffectView alloc]initWithEffect:blurEffect];
effectView.frame = CGRectMake(imageView.frame.size.width/2,0,
imageView.frame.size.width/2, imageView.frame.size.height);
[self.view addSubview:effectView];
UIBlurEffect.png