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];
  • 相关阅读:
    spring+hibernate常见异常集合
    Java报错原因汇总
    java常见异常集锦
    连接池 druid(阿里巴巴的框架)
    企业支付宝账号开发接口实现
    Maven使用常见问题整理
    MySQL的分页
    Struts2中通配符的使用
    Centos下安装mysql 总结
    将linux用在开发环境中
  • 原文地址:https://www.cnblogs.com/YangFuShun/p/6544616.html
Copyright © 2011-2022 走看看