且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IOS毛玻璃效果

更新时间:2022-04-26 00:51:43

最近实习公司的ios项目中需要用到毛玻璃的效果(如下图效果)

IOS毛玻璃效果

尝试用了几个方法都不太满意,最后终于找到了一个比较好的解决方案。

在IOS8以上的机器中,我们可以利IOS SDK中已经提供了的UIBlurEffect和UIVisualView实现简单毛玻璃的效果。实现的方法如下:

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"originalImage.jpg"]];
    [self.view addSubview:imageView];
    imageView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width/2, imageView.frame.size.height/2)];
    [visualEffectView setEffect:blurEffect];
    [self.view addSubview:visualEffectView];

其中visualEffectView就相当于“毛玻璃片”,其能够对其后面所盖着的视图进行毛玻璃效果处理并透射过来。我们也可以通过修改blurEffect的模式来调整“毛玻璃样式”,总共有三种模式可以选择

UIBlurEffectStyleExtraLight

UIBlurEffectStyleLight

UIBlurEffectStyleDark

比较遗憾的是,苹果只为提供了调整“毛玻璃样式”的方法,却没有为提供调整“毛玻璃程度”的方法。

该方法的demo可见我的github:

OboBlurViewDemo


swift版参考链接:

http://blog.csdn.net/pucker/article/details/43405929