更新时间:2022-04-26 00:51:43
最近实习公司的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];
UIBlurEffectStyleExtraLight
UIBlurEffectStyleLight
UIBlurEffectStyleDark
比较遗憾的是,苹果只为提供了调整“毛玻璃样式”的方法,却没有为提供调整“毛玻璃程度”的方法。
该方法的demo可见我的github:
swift版参考链接: