iOS通過(guò)幾種不同的方式實(shí)現(xiàn)毛玻璃效果

需求

在我們開發(fā)中有時(shí)會(huì)遇到這樣的要求,彈出一個(gè)半透明層,這樣可以看到后面的頁(yè)面,使得我們的APP層次更加清晰,那么如何實(shí)現(xiàn)這一半透明層呢,我用了4種方式,接下來(lái)一一呈現(xiàn)。

解決方案

這里有代碼的Demo

首先我們看一下原圖:


origin.png

1.UIBlurEffect

先看效果圖:


UIBlurEffect.png

UIBlurEffect是iOS8以后才出的,那么如果系統(tǒng)還沒升級(jí)到8呢,雖然這是極少數(shù),但是我們?cè)陂_發(fā)中基本都是要考慮到的,對(duì)于這類用戶,我們可以用UIToolbar來(lái)解決,下面是實(shí)現(xiàn)的代碼:

if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0f) {
            
            UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
            UIVisualEffectView *BlurView = [[UIVisualEffectView alloc] initWithEffect:blur];
            ((UIVisualEffectView *)BlurView).frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            BlurView.alpha = 0.7;
            [self addSubview:BlurView];
            [self sendSubviewToBack:BlurView];
            
}else {
            
            UIToolbar *BlurView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
            ((UIToolbar *)BlurView).barStyle = UIBarStyleBlack;
            BlurView.alpha = 0.7;
            [self addSubview:BlurView];
            [self sendSubviewToBack:BlurView];
            
        }

2.GPUImage

先看效果圖:


GPUImage.png

GPUImage是一款功能強(qiáng)大的庫(kù),其中的各種濾鏡效果真的讓人眼花繚亂,用它做一個(gè)半透明層確實(shí)很輕松,不過(guò)要想用得好還是要下一番功夫的,代碼也很簡(jiǎn)單,我們首先創(chuàng)建一個(gè)繼承自GPUImageView的View,然后加上如下代碼就可以了:

GPUImageGaussianBlurFilter * blurFilter = [[GPUImageGaussianBlurFilter alloc] init];
GPUImageView *filterView = (GPUImageView *)self;
[blurFilter addTarget:filterView];

3.蘋果自己封裝的模糊效果

先看效果圖:

Apple.png

如果僅僅是要做一個(gè)半透明的模糊效果的話,有可能會(huì)嫌GPUImage太龐大,其實(shí)蘋果的官方Demo中自己就封裝了一個(gè),網(wǎng)址在這里,如果網(wǎng)址失效的話,可以在Apple Developer中搜索UIImageEffects,將UIImageEffects的.h和.m文件導(dǎo)進(jìn)來(lái),只要加上如下代碼,就可以實(shí)現(xiàn)效果了:

@property (strong, nonatomic) CALayer *blurLayer;
self.blurLayer = [CALayer layer];
self.blurLayer.frame = self.bounds;
[self.layer addSublayer:self.blurLayer];
UIImage *image = [UIImageEffects imageByApplyingLightEffectToImage:nil];
self.blurLayer.contents = (__bridge id)(image.CGImage);

4.添加一個(gè)半透明的controller

先看效果圖:


BlurController.png

上面三種都是添加了一個(gè)View,可能有人會(huì)想要彈出一個(gè)半透明的controller,其實(shí)也比較簡(jiǎn)單,添加如下代碼即可:

BlurViewController *blurController = [[BlurViewController alloc]initWithNibName:@"BlurViewController" bundle:[NSBundle mainBundle]];
blurController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
blurController.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;
        
[self presentViewController:blurController animated:YES completion:^{
    blurController.view.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
}];

UIModalPresentationOverCurrentContext也是iOS8以后出的,那么如果之前的版本該怎么辦呢,參考這里

最后還是代碼的Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1:成功前必須要明白的道理(微雅禪秀 追風(fēng)) 首先這個(gè)標(biāo)題就能博取砸門群大多數(shù)人的興趣,我們不都是渴望成功嗎?羅胖...
    指南針1閱讀 480評(píng)論 0 0
  • 誒,起啥名啊,一起就是個(gè)瑪麗蘇,一瑪麗蘇就是黑歷史,一黑歷史就難以為繼,憋起名了,他,人也。真正的粉絲,即使我不說(shuō)...
    三笑一癡閱讀 306評(píng)論 0 0