zoukankan      html  css  js  c++  java
  • iOS

      iOS - 毛玻璃效果

    iOS8之后苹果提供了制作毛玻璃效果的API

    就是这个UIVisualEffectView,用这个initWithEffect:去初始化,然后呢,他有两种效果继承自UIVisualEffect。这个父类不用管,什么也不做,主要看他的两个子类UIBlurEffect和UIVibrancyEffect。

    UIBlurEffect : 这个是影响毛玻璃后面视图的

    效果图:

    这里写图片描述

    UIVibrancyEffect : 这个是影响毛玻璃上的视图的

    这里写图片描述

    是不是很漂亮,做起来也不难呢。

    先说毛玻璃下面的效果的做法。

    你先初始化一个UIBlurEffect对象,他有三种风格

    typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {

    UIBlurEffectStyleExtraLight,

    UIBlurEffectStyleLight,

    UIBlurEffectStyleDark

    }

    上面的效果都是用UIBlurEffectStyleLight做出来的

    UIBlurEffectStyleExtraLight效果如下

    这里写图片描述

    UIBlurEffectStyleDark效果如下

    这里写图片描述

    然后呢用这个UIBlurEffect对象创建一个UIVisualEffectView对象。用你需要被虚化的视图添加这个UIVisualEffectView对象为子视图就可以了。

    接着说毛玻璃上面的效果

    创建一个UIVibrancyEffect对象,用之前创建的blur去初始化,然后创建一个UIVisualEffectView对象,用这个UIVibrancyEffect对象初始化,最后将你想要添加的子视图添加到UIVisualEffectView的contentView上就可以了。

    完整代码如下:

    // UIBlurEffect效果
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = [UIImage imageNamed:@"pic"];
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *ev = [[UIVisualEffectView alloc] initWithEffect:blur];
    ev.frame = self.view.frame;
    [imageView addSubview:ev];
    
    // UIVibrancyEffect效果
    UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blur];
    UIVisualEffectView *ano = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
    ano.frame = self.view.frame;
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:40];
    label.frame = CGRectMake(100, 100, 400, 100);
    label.text = @"Beautiful View";
    [ev.contentView addSubview:ano];
    [ano.contentView addSubview:label];
    
    [self.view addSubview:imageView];
  • 相关阅读:
    HDOJ 2011
    OpenCV学习笔记(1)——VS2010 下安装OpenCV 2.4.4安装
    OC——Foundation—常用的类(2)----NSArray
    OC——Foundation—常用的类(1)字符串(NSString)
    OC——Foundation—结构体
    OC----Block语法
    OC----protocol语法
    OC----初识
    OC----内存管理
    OC----Category语法
  • 原文地址:https://www.cnblogs.com/YangFuShun/p/6544616.html
Copyright © 2011-2022 走看看