美文网首页
iOS之毛玻璃模糊

iOS之毛玻璃模糊

作者: Smallwolf_JS | 来源:发表于2018-07-27 19:52 被阅读83次

首先需要了解相关的四个类
UIVisualEffect
UIBlurEffect
UIVibrancyEffect
UIVisualEffectView

继承关系分别是继承关系

UIVisualEffect : NSObject
 |—> UIBlurEffect //继承自UIVisualEffect
 |—> UIVibrancyEffect //继承自UIVisualEffect
UIVisualEffectView : UIView

1.关于UIBlurEffect
我们首先看UIBlurEffect类,Apple文档中只给出了一个方法:
+ (UIBlurEffect *)effectWithStyle:(UIBlurEffectStyle)style;
我们实现也是这样:
模糊效果的三种风格

@param UIBlurEffectStyle
     
       UIBlurEffectStyleExtraLight,//额外亮度,(高亮风格)
       UIBlurEffectStyleLight,//亮风格
       UIBlurEffectStyleDark//暗风格
//实现模糊效果
UIBlurEffect *blur =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *vise = [[UIVisualEffectView alloc] initWithEffect:blur];

2、关于UIVibrancyEffect
文档中给出的也是一个方法:
+ (UIVibrancyEffect *)effectForBlurEffect:(UIBlurEffect *)blurEffect;
UIVibrancyEffect的作用是放大和调整UIVisualEffectView内容视图的内容的颜色,让UIVisualEffectViewcontentView中的内容看起来更加生动。它作为一个子视图被放置在UIVisualEffectView上面,去连接UIBlurEffect。这种效果只会影响添加到UIVisualEffectViewcontentView上的内容。因为活力影响是受颜色依赖的.....

我们可以看出:通常UIVibrancyEffect对象是与UIBlurEffect一起使用,主要用于处理在UIBlurEffect特效上的一些显示效果。
代码:

//    UIBlurEffectStyleProminent//更改此处可以更改文字颜色
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleProminent];
    UIVibrancyEffect *vibrancy = [UIVibrancyEffect effectForBlurEffect:blur];
    UIVisualEffectView *vise = [[UIVisualEffectView alloc] initWithEffect:vibrancy];
    //设置虚化度
    vise.alpha=0.9;
    vise.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);
    
    UIImageView * imageView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lauren.jpg"]];
    imageView2.frame = CGRectMake(0, self.view.frame.size.height - 300, self.view.frame.size.width, 300);
    [imageView2 addSubview:vise];

在contentView中添加Label

 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, self.view.frame.size.width, 21)];
    label.text  = @"这就是文字,用来查看效果";
    [label setTextColor:[UIColor redColor]];//textColor在这里失效
    
//    这也是特别要注意的地方,文档中也有专门提出,并给出了解释:
Because the vibrancy effect is color dependent, subviews added to 
the contentView need to be tintColorDidChange aware and must be 
prepared to update themselves accordingly.
// 所以我们使用 label.textColor去改变颜色是完全不起作用的
    label.tintColor = [UIColor greenColor];
    label.font = [UIFont systemFontOfSize:17];
    [label setTextAlignment:NSTextAlignmentCenter];
    ///不要直接添加子视图去UIVisualEffectView,而是要添加到contentView上。
    [vise.contentView addSubview:label];
    [self.view addSubview:imageView2];

3、UIVisualEffectView
老规矩先看文档:也是寥寥的四种,其中值得一提的是:contentView。这里明确告诉我们:不要直接添加子视图去UIVisualEffectView,而是要添加到contentView上。

出自Xcode

@property (nonatomic, strong, readonly) UIView *contentView; // Do not add subviews directly to UIVisualEffectView, use this view instead.
@property (nonatomic, copy, nullable) UIVisualEffect *effect;
- (instancetype)initWithEffect:(nullable UIVisualEffect *)effect NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
NS_ASSUME_NONNULL_END

UIBlurEffect是对整个背景进行虚化,UIVibrancyEffect是对添加的标签等附件进行背景虚化

最终效果:


代码:https://github.com/SmallwolfiOS/UIVisualEffectViewDemo.git

相关文章

网友评论

      本文标题:iOS之毛玻璃模糊

      本文链接:https://www.haomeiwen.com/subject/irvgmftx.html