毛玻璃的實現(xiàn)一般要用到以下幾個類:
- UIBlurEffect
- UIVibrancyEffect
- UIVisualEffectView
1割去、第一種毛玻璃效果
-(void)VisualEffectView{
//先生成圖片背景
UIImageView *blurImag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
blurImag.image= [UIImageimageNamed:@"homePage2"];
[self.viewaddSubview:blurImag];
UILabel*blurLabel = [[UILabelalloc]initWithFrame:CGRectMake(30,60,self.view.frame.size.width-60,300)];
blurLabel.text = @"Our mind is sponge, our heart is stream.";
/** 設置blurLabel的最大行數(shù). */
blurLabel.numberOfLines=2;
/** 設置blurLabel的字體顏色. */
blurLabel.textColor= [UIColorwhiteColor];
/** 設置blurLabel的字體為系統(tǒng)粗體, 字體大小為34. */
blurLabel.font= [UIFontboldSystemFontOfSize:34];
/** 創(chuàng)建UIBlurEffect類的對象blur, 參數(shù)以UIBlurEffectStyleLight為例. */
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
/** 創(chuàng)建UIVisualEffectView的對象visualView, 以blur為參數(shù). */
UIVisualEffectView * visualView = [[UIVisualEffectView alloc] initWithEffect:blur];
/** 將visualView的大小等于blurImageView的大小. (visualView的大小可以自行設定, 它的大小決定了顯示毛玻璃效果區(qū)域的大小.) */
visualView.frame= blurImag.bounds;
visualView.alpha=1;
/** 將visualView添加到blurImageView上. */
[blurImag addSubview:visualView];
[visualView.contentView addSubview:blurLabel];
}
Simulator Screen Shot - iPhone 8 - 2018-05-08 at 13.51.24.png
2、第二種效果
-(void)VisualEffectView2{
/** 1. 創(chuàng)建UIImageView的對象vibrancyImageView. */
UIImageView *vibrancyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 2*self.view.frame.size.width, self.view.frame.size.height)];
UIImage *image = [UIImage imageNamed:@"homePage2"];
/**
* 在UIVibrancyEffect這種類型的毛玻璃中, 蘋果官方API對UIImageView的image屬性的渲染方式做出了要求:
* UIImageView will need its image to have a rendering mode of UIImageRenderingModeAlwaysTemplate to receive the proper effect.
*
* UIImageView的屬性imgae的渲染方式要選擇UIImageRenderingModeAlwaysTemplate, 會獲得更好的效果.
*/
/**
* 給UIImage類的對象設置渲染方式的方法: - (UIImage * nonnull)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
* 參數(shù)renderingMode: typedef enum : NSInteger {
UIImageRenderingModeAutomatic,
UIImageRenderingModeAlwaysOriginal,
UIImageRenderingModeAlwaysTemplate,
information
} UIImageRenderingMode;
*/
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
vibrancyImageView.image = image;
vibrancyImageView.userInteractionEnabled = YES;
[self.view addSubview:vibrancyImageView];
/** 2. 創(chuàng)建UILable的對象. */
UILabel *vibrancyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width - 20, 300)];
vibrancyLabel.text = @"Our mind is a sponge, our heart is a stream.";
vibrancyLabel.numberOfLines = 2;
vibrancyLabel.textColor = [UIColor whiteColor];
vibrancyLabel.font = [UIFont boldSystemFontOfSize:34];
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
/**
* 在上面我們提到過UIVisualEffectView類的類方法里的參數(shù)effect有兩種類型:UIBlurEffect和UIVibrancyEffect.
* 在這里我們創(chuàng)建UIVibrancyEffect類型的UIvisualEffectView;
*/
/**
* UIVibrancyEffect有兩種創(chuàng)建方法, 在這里我們使用: + (UIVibrancyEffect *)effectForBlurEffect:(UIBlurEffect *)blurEffect
*
* 我們可以看出類方法中的參數(shù)類型是UIBlurEffect類型, 所以之前創(chuàng)建了一個UIBlurEffect的對象.
*/
UIVibrancyEffect *vibrancy = [UIVibrancyEffect effectForBlurEffect:blur];
/** 下面的步驟同上. */
UIVisualEffectView * visualView = [[UIVisualEffectView alloc] initWithEffect:vibrancy];
visualView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
visualView.alpha = 1;
[vibrancyImageView addSubview:visualView];
[visualView.contentView addSubview:vibrancyLabel];
}
Simulator Screen Shot - iPhone 8 - 2018-05-08 at 13.50.27.png