似乎目前所見(jiàn)這樣的效果都是在金融類(lèi)App中,畢竟錢(qián)真的很重要??。在此嘗試一下,反正閑著也是閑著(?)。
可知雙擊Home鍵或者應(yīng)用退到后臺(tái)的時(shí)候,都會(huì)經(jīng)歷以下方法:
- (void)applicationWillResignActive:(UIApplication *)application { }
所以在這里添加毛玻璃效果,并且在
- (void)applicationDidBecomeActive:(UIApplication *)application { }
方法中刪除此效果。
步驟在此
- 獲取當(dāng)前VC
- (UIViewController *)currentVC {
UIViewController *currentVC = nil;
if (self.window.windowLevel != UIWindowLevelNormal) {
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *window in windows) {
if (window.windowLevel == UIWindowLevelNormal) {
self.window = window;
break;
}
}
}
UIView *frontView = [[self.window subviews] lastObject];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
currentVC = nextResponder;
} else {
currentVC = self.window.rootViewController;
}
return currentVC;
}
- 截取當(dāng)前視圖
- (UIImage *)snapShot:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- 添加毛玻璃效果
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"applicationWillResignActive");
// 毛玻璃效果
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *vc = [self currentVC];
imageView.image = [self snapShot:vc.view];
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
effectView.frame = [UIScreen mainScreen].bounds;
[imageView addSubview:effectView];
view.tag = 1000;
[view addSubview:imageView];
[self.window addSubview:view];
}
- 在應(yīng)用DidBecomeActive時(shí)去除效果
- (void)applicationDidBecomeActive:(UIApplication *)application {
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
if (window.windowLevel == UIWindowLevelNormal) {
UIView *view = [window viewWithTag:1000];
[view removeFromSuperview];
}
}
}
以上。