項目中有時為了安全起見,在雙擊Home鍵,退到不活躍狀態(tài)時,需要添加模糊效果,比如下圖:
高斯模糊
要實現(xiàn)這種效果,非常簡單,只需在APPdelegate里面添加幾句代碼即可
//AppDelegate.h中添加以下屬性
@property (nonatomic,strong) UIVisualEffectView *visualEffectView;//毛玻璃效果
AppDelegate.m中
- (void)applicationWillResignActive:(UIApplication *)application {
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
self.visualEffectView.alpha = 0;
self.visualEffectView.frame = self.window.frame;
[self.window addSubview:self.visualEffectView];
[UIView animateWithDuration:0.2 animations:^{
self.visualEffectView.alpha = 1;
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[UIView animateWithDuration:0.2 animations:^{
self.visualEffectView.alpha = 0;
} completion:^(BOOL finished) {
[self.visualEffectView removeFromSuperview];
}];
}
以上就是實現(xiàn)毛玻璃的全部代碼,是不是很簡單呢~~~
后續(xù)加上:經(jīng)測試這種寫法有點小問題,就是如果項目中有撥打電話的操作(系統(tǒng)彈出撥打電話的提示框),這時彈出框下面的整個屏幕也是毛玻璃效果,這并不是我們想看到的,那怎么改呢,其實就是方法寫的位置不對,按照下面的方法寫
注意:將
- (void)applicationWillResignActive:(UIApplication *)application;
里面的代碼放進
- (void)applicationDidEnterBackground:(UIApplication *)application;
里面,兩個方法里面的代碼雖然相似,但是屬性值有所改變,要注意!!!
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
self.visualEffectView.alpha = 1;
self.visualEffectView.frame = self.window.frame;
[self.window addSubview:self.visualEffectView];
[UIView animateWithDuration:0.2 animations:^{
self.visualEffectView.alpha = 1;
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[UIView animateWithDuration:0.2 animations:^{
self.visualEffectView.alpha = 0;
} completion:^(BOOL finished) {
[self.visualEffectView removeFromSuperview];
}];
}
改成下面的方法后,上面出現(xiàn)的問題就解決了,只是可能首次進入后臺的時候不能正常顯示效果,支付寶也存在這種情況,但是基本上無傷大雅~~~