iOS部分毛玻璃模糊效果

791 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第4天,点击查看活动详情

背景

  • 左右跳转,跳转完之后,右的是视图要保留部分左边视图的模糊背景。
  • 转化为程序语言, 一个scollview上放两个uiview, scollview的contentWidth 为2倍的屏宽-中间模糊的宽度blurry_Width. leftView宽度为screenWidth, rightView宽度为 screenWidth - blurry_width,进行跳转的时候 scollview的contentOffset 为 screenWidth - blurry_width. 这样正好,rightview出现的时候,左边还有blurry_width得leftView。 这时候再把左边视图的 rect(screenwidth - blurry_width, 0,blurry_width, screenHeight) 这个部分的变成模糊视图,问题就解决了。
_FGView = [[GJBaseView alloc]initWithFrame:CGRectMake(SCREENWIDTH - width, 0, width, SCREENHEIGHT)];

        _FGView.backgroundColor = [UIColor clearColor];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:**self** action: **@selector**(tapBack)];

        [_FGView addGestureRecognizer:tap];

//        _FGView.backgroundColor = TESTCOLOR;

        _FGView.hidden = **YES**;

  • 背景图大小。然后把背景图模糊,就能完成了。

问题出现

  • 模糊的是纯背景图,看上去是有个蒙层的效果,但是底部的视图没有模糊。UI需要的效果是leftview最右边的那一部分。所以还是不能取巧。那就截取左边视图的一部分, 然后生成image, 再在FGView(背景图)加上一个uiimageView。 模糊imageView, 这样才能过UI的关
  • 首先截取指定位置的view 生成image
+ (UIImage *)imageFromView: (UIView *) theView  atFrame:(CGRect)r

{

    UIGraphicsBeginImageContext(theView.frame.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    UIRectClip(r);

    [theView.layer renderInContext:context];

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    **return**  theImage;

}

  • 然后就是把uiimageView进行模糊操作
//普通毛玻璃

+ (**void**)toolbar:(UIImageView *)bgImgView frame:(CGRect)frame{

    /**

     iOS7.0

     毛玻璃的样式(枚举)

     UIBarStyleDefault          = 0,

     UIBarStyleBlack            = 1,

     UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack

     UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES

     */

    UIToolbar *toolbar = [[UIToolbar alloc] init];

    toolbar.alpha = 0.98;

    toolbar.frame = frame;

    toolbar.barStyle = UIBarStyleDefault;

    [bgImgView addSubview:toolbar];

}

网上找了一下资料

  • 高斯模糊
//高斯模糊

+ (**void**)blurEffect:(UIImageView *)bgImgView frame:(CGRect)frame {

    /**

     iOS8.0

     毛玻璃的样式(枚举)

     UIBlurEffectStyleExtraLight,

     UIBlurEffectStyleLight,

     UIBlurEffectStyleDark,

     

     // iOS 10新增的枚举值

     UIBlurEffectStyleRegular NS_ENUM_AVAILABLE_IOS(10_0), // Adapts to user interface style

     UIBlurEffectStyleProminent NS_ENUM_AVAILABLE_IOS(10_0), // Adapts to user interface style

     */

    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];

    effectView.frame = frame;

    [bgImgView addSubview:effectView];

    

    // 加上以下代码可以使毛玻璃特效更明亮点

    UIVibrancyEffect *vibrancyView = [UIVibrancyEffect effectForBlurEffect:effect];

    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyView];

    visualEffectView.translatesAutoresizingMaskIntoConstraints = **NO**;

    [effectView.contentView addSubview:visualEffectView];

}
  • 最近项目比较急, 没来得及花时间去研究。还有很多可以改进的地方, 先暂时记录下来吧,《程序员的自我修养》里面有这么一句话 “当你突然有一个有趣的想法的时候,就拿笔记下来,可能你当时就是随便一想,等你闲下来的时候,再看到这个想法,说不定可以给你带来新的东西” 原话我记不太全,大概是这个意思。 所以即使准备的不充分,我还是把这些记录下来,等哪天看到了,再继续这方面的探索