美文网首页
ios毛玻璃效果实现

ios毛玻璃效果实现

作者: 豆里丸 | 来源:发表于2018-05-08 13:53 被阅读24次

毛玻璃的实现一般要用到以下几个类:

  • 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的最大行数. */

    blurLabel.numberOfLines=2;

    /** 设置blurLabel的字体颜色. */

    blurLabel.textColor= [UIColorwhiteColor];

    /** 设置blurLabel的字体为系统粗体, 字体大小为34. */

    blurLabel.font= [UIFontboldSystemFontOfSize:34];


    /** 创建UIBlurEffect类的对象blur, 参数以UIBlurEffectStyleLight为例. */

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    /** 创建UIVisualEffectView的对象visualView, 以blur为参数. */

    UIVisualEffectView * visualView = [[UIVisualEffectView alloc] initWithEffect:blur];

    /** 将visualView的大小等于blurImageView的大小. (visualView的大小可以自行设定, 它的大小决定了显示毛玻璃效果区域的大小.) */

    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. 创建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
     * 参数renderingMode: typedef enum : NSInteger {
     UIImageRenderingModeAutomatic,
     UIImageRenderingModeAlwaysOriginal,
     UIImageRenderingModeAlwaysTemplate,
     information
     } UIImageRenderingMode;
     */
    [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    
    vibrancyImageView.image = image;
    
    vibrancyImageView.userInteractionEnabled = YES;
    
    [self.view addSubview:vibrancyImageView];
    
    /** 2. 创建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类的类方法里的参数effect有两种类型:UIBlurEffect和UIVibrancyEffect.
     * 在这里我们创建UIVibrancyEffect类型的UIvisualEffectView;
     */
    
    /**
     * UIVibrancyEffect有两种创建方法, 在这里我们使用: + (UIVibrancyEffect *)effectForBlurEffect:(UIBlurEffect *)blurEffect
     *
     * 我们可以看出类方法中的参数类型是UIBlurEffect类型, 所以之前创建了一个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

相关文章

  • 实现div毛玻璃背景

    原文在我的博客☞实现div毛玻璃背景 毛玻璃效果 ios里毛玻璃效果的使用非常多,本文介绍一个实现div毛玻璃背景...

  • iOS之原生毛玻璃效果

    iOS 8之后 iOS提供了实现原生毛玻璃效果的接口,使用起来也非常方便,先简单看看效果图 使用方法 在添加毛玻璃...

  • 安卓模糊效果探讨

    在项目中我们或多或少的需要根据产品的要求,实现类似于ios的毛玻璃效果。在ios中毛玻璃的实现,比较简单,系统提供...

  • iOS-小Demo--下拉放大顶部图片+毛玻璃效果

    记录一个简单的下拉顶部图片放大的效果,再加个毛玻璃! iOS8之后毛玻璃效果实现:利用 UIVisualEffec...

  • Html开发-安卓毛玻璃无效果解决办法

    毛玻璃实现代码: filter:blur(value); value为毛玻璃模糊程度。 该效果在Ios开无任何问...

  • 高斯模糊

    高斯模糊 【iOS 开发】实现毛玻璃(高斯模糊)效果 - CocoaChina_让移动开发更简单

  • iOS毛玻璃效果的实现方法

    ios开发中常常用到的毛玻璃效果实现方法 iOS8以后使用系统里的UIBlurEffect可以实现,UIBlurE...

  • UIVisualEffectView 毛玻璃效果

    UIVisualEffectView是从iOS 8开始提供的控件,功能是创建毛玻璃(Blur)效果,也就是实现模糊...

  • ios实现毛玻璃效果

    我们开发中可能经常会使用到给图片添加毛玻璃效果,ios7以后毛玻璃效果就开始比较多的使用了,系统提供了一种简单实现...

  • iOS 实现毛玻璃效果

    网上看到一遍文章,感觉写的挺清爽的,想收藏,但是又怕链接失效,所以在这里备份一下,顺便给作者点个赞;原文地址:ht...

网友评论

      本文标题:ios毛玻璃效果实现

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