您的位置:首页 > 移动开发 > IOS开发

iOS在后台情况下展现出模糊效果(双击home键,看到的应用程序模糊)

2016-09-07 10:37 330 查看

iOS后台模糊效果(适合金融类app,安全性可极大提高)

本人不经常更新博客,对于百度,谷歌一搜就能找到的基础知识一般不发表在博客上。

开始写怎么实现

在appdelegate的- (void)applicationWillEnterForeground:(UIApplication *)application方法中添加方法,每次应用后台切换到前台都会走这个方法,在下做了一些判断直有在开启并支持指纹识别和已有用户登陆的情况下才可以使用:

在appdelegate--- (void)applicationWillResignActive:(UIApplication *)application方法中

- (void)applicationWillResignActive:(UIApplication *)application {

//    判断是否存在用户登陆,没有用户信息就不遮盖了

    

    if (!([[NSUserDefaults standardUserDefaults] stringForKey:@"phone"] == nil)){

    //进入后台盖上view,实现模糊效果

    

    UIView* view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    

    UIImageView *imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    //获取当前视图控制器

    UIViewController *vc = [self getCurrentVC];

    //截取当前视图为图片

    imageV.image = [self snapshot:vc.view];

    

//添加毛玻璃效果

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];

    effectview.frame = [UIScreen mainScreen].bounds;

    [imageV addSubview:effectview];

    view.tag = 1111;

    [view addSubview:imageV];

    [self.window addSubview:view];   

    }  

}

//获取当前屏幕显示的viewcontroller

- (UIViewController *)getCurrentVC

{

    UIViewController *result = nil;

    

    

    if (self.window.windowLevel != UIWindowLevelNormal)

    {

        NSArray *windows = [[UIApplication sharedApplication] windows];

        for(UIWindow * tmpWin in windows)

        {

            if (tmpWin.windowLevel == UIWindowLevelNormal)

            {

                self.window = tmpWin;

                break;

            }

        }

    }

    

    UIView *frontView = [[self.window subviews] lastObject];

    id nextResponder = [frontView nextResponder];

    

    if ([nextResponder isKindOfClass:[UIViewController class]]){

        result = nextResponder;

    }

//    else{

//        result = self.window.rootViewController;

//    }

    return result;

}

//截取当前视图为图片

- (UIImage *)snapshot:(UIView *)view

{

    

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);

    

    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

       return image;

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 安全 金融 app