项目总结五:iOS给项目在后台添加模糊效果
项目在开发过程中,由于安全问题,需要让项目在后台时候添加模糊处理。并且在前台的时候把模糊处理关掉。
后台处理
- (UIViewController*)getCurrentVC
{UIViewController*result =nil;
if(self.window.windowLevel!=UIWindowLevelNormal)
{
NSArray*windows = [[UIApplicationsharedApplication]windows];
for(UIWindow* tmpWininwindows)
{
if(tmpWin.windowLevel==UIWindowLevelNormal)
{
self.window= tmpWin;
break;
}}}
UIView*frontView = [[self.windowsubviews]lastObject];
idnextResponder = [frontViewnextResponder];
if([nextResponderisKindOfClass:[UIViewControllerclass]]){
result = nextResponder;}
//else{
//result = self.window.rootViewController;
//}
returnresult;}
//截取当前视图为图片
- (UIImage*)snapshot:(UIView*)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES,0);
[viewdrawViewHierarchyInRect:view.boundsafterScreenUpdates:YES];
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
- (void)applicationWillResignActive:(UIApplication*)application
{
//盖上view
//进入后台盖上view,实现模糊效果
UIView* view = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
UIImageView*imageV = [[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
//获取当前视图控制器
UIViewController*vc = [selfgetCurrentVC];
//截取当前视图为图片
imageV.image= [selfsnapshot:vc.view];
//添加毛玻璃效果
UIBlurEffect*blur = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleExtraLight];
UIVisualEffectView*effectview = [[UIVisualEffectViewalloc]initWithEffect:blur];
effectview.frame= [UIScreenmainScreen].bounds;
effectview.alpha=0.9f;
[imageVaddSubview:effectview];
view.tag=1111;
[viewaddSubview:imageV];
[self.windowaddSubview:view];
}
前台处理:
- (void)applicationDidBecomeActive:(UIApplication*)application {
debugLog(@"app did become active.\n");
//移除
NSArray* array = [[UIApplicationsharedApplication]keyWindow].subviews;
for(idviewinarray)
{
if([viewisKindOfClass:[UIViewclass]])
{
UIView* myView = view;
if(myView.tag==1111)
{
[myViewremoveFromSuperview];
}}}}