zoukankan      html  css  js  c++  java
  • iOS模糊效果(毛玻璃效果)的实现

      前一段时间项目中用到毛玻璃效果,那时对UIBlurEffect类和 UIVisualEffectView这两个类做了一部分了解。但当时并没有去特别的深入研究,直到项目做完后,才静下心来好好研究了一番。记录一下。

      iOS8之后,Apple新添加UIBlurEffect类、UIVibrancyEffect类 和 UIVisualEffectView类这三种类,用途就是对背景色进行模糊化,也就是我们称的 "毛玻璃效果"。接下来就对具体的使用做一下分析吧。

      其实细看下来,Apple对这种特效封装的很好,所以我们使用起来的并不需要什么步骤。不得不佩服Apple的强大啊。

     1、关于UIBlurEffect类

    我们首先看UIBlurEffect类,Apple文档中只给出了一个方法:

    + (UIBlurEffect *)effectWithStyle:(UIBlurEffectStyle)style;

    我们实现也是这样:

     /**
         *   模糊效果的三种风格
         *
         *  @param UIBlurEffectStyle
         
           UIBlurEffectStyleExtraLight,//额外亮度,(高亮风格)
           UIBlurEffectStyleLight,//亮风格
           UIBlurEffectStyleDark//暗风格
         *
         */
        //实现模糊效果
        UIBlurEffect *blurEffrct =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

     2、关于UIVibrancyEffect类

    文档中给出的也是一个方法:

    + (UIVibrancyEffect *)effectForBlurEffect:(UIBlurEffect *)blurEffect;

    官方给出的解释是这样的

    /* UIVibrancyEffect amplifies and adjusts the color of content layered behind the view, allowing content placed inside the contentView to become more vivid. It is intended to be placed over, or as a subview of, a UIVisualEffectView that has been configured with a UIBlurEffect. This effect only affects content added to the contentView. 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. UIImageView will need its image to have a rendering mode of UIImageRenderingModeAlwaysTemplate to receive the proper effect.

     */

    翻译如下:

    UIVibrancyEffect的作用是放大和调整UIVisualEffectView内容视图的内容的颜色,让UIVisualEffectView的contentView中的内容看起来更加生动。它作为一个子视图被放置在UIVisualEffectView上面,去连接UIBlurEffect。这种效果只会影响添加到UIVisualEffectView的contentView上的内容。因为活力影响是受颜色依赖的.....

     

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

    下面看看实现代码:

    //实现模糊效果
        UIBlurEffect *blurEffrct =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffrct];
        
        UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:vibrancyEffect];
       // visualEffectView.backgroundColor = [ UIColor grayColor ];

        visualEffectView.contentView.frame = CGRectMake(10, 100, 300, 500);

    
         [self.view addSubview:visualEffectView];

     

    下面我们往 UIVisualEffectView 的contentView上添加个view看看效果

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 300, 280, 90)];
        
        label.text = @"曾经撒次考试了hhhhhhhhhhhhhhh";
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:30];
        label.tintColor = [UIColor yellowColor];
        label.numberOfLines = 0;
        [visualEffectView.contentView addSubview:label];

     

    上面代码中可以看到, 我改变Label中text的颜色是使用的:tintColor ,这也是特别要注意的地方,文档中也有专门提出,并给出了解释: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去改变颜色是完全不起作用的。

    运行效果图如下:(只剪切出效果部分)

    至于颜色不是设置的yellowColor,我想不需要多说了吧,这就是UIVibrancyEffect的功能。

    3、UIVisualEffectView类

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

    @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;

     

    这里我就给出一个比较完整的代码(我们看一看UIBlurEffect类 和 UIVisualEffectView类 的效果):

        
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"6.jpg"]];
        /**
         *   模糊效果的三种风格
         *
         *  @param UIBlurEffectStyle
         
           UIBlurEffectStyleExtraLight,//额外亮度,(高亮风格)
           UIBlurEffectStyleLight,//亮风格
           UIBlurEffectStyleDark//暗风格
         *
         */
        //实现模糊效果
        UIBlurEffect *blurEffrct =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        //毛玻璃视图
        UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffrct];
        
        visualEffectView.frame = CGRectMake(60, 100, 300, 500);
        
        visualEffectView.alpha = 0.9;
    
        [self.view addSubview:visualEffectView];

     

    看看效果图是不是你想要的:

    关于iOS8之前的实现,可以去github上看看一些封装库。有很多不错的三方库不错,这里就不列出了。

  • 相关阅读:
    POJ 1775 (ZOJ 2358) Sum of Factorials
    POJ 1844 Sum
    HDOJ 1081(ZOJ 1074) To The Max(动态规划)
    HDOJ 2012 素数判定
    HDOJ 2011 多项式求和
    HDOJ 2010 水仙花数
    马云最新发言:让员工、客户、合作伙伴比自己更强
    乐视手机1S正式发售,乐视商城官网抽风遭网友吐槽
    C++引用(Reference)
    实验三 二叉树的基本操作(建立)及遍历
  • 原文地址:https://www.cnblogs.com/LQCQ-Silent/p/5901462.html
Copyright © 2011-2022 走看看