美文网首页
双击Home键实现模糊效果

双击Home键实现模糊效果

作者: 倾兰特 | 来源:发表于2016-10-19 17:23 被阅读64次

    似乎目前所见这样的效果都是在金融类App中,毕竟钱真的很重要😳。在此尝试一下,反正闲着也是闲着(?)。

    可知双击Home键或者应用退到后台的时候,都会经历以下方法:

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

    所以在这里添加毛玻璃效果,并且在

    - (void)applicationDidBecomeActive:(UIApplication *)application { }
    

    方法中删除此效果。

    步骤在此

    • 获取当前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;
    }
    
    • 截取当前视图
    - (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];
    }
    
    • 在应用DidBecomeActive时去除效果
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        
        for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
            
            if (window.windowLevel == UIWindowLevelNormal) {
                
                UIView *view = [window viewWithTag:1000];
                [view removeFromSuperview];
            }
        }
    }
    

    以上。

    相关文章

      网友评论

          本文标题:双击Home键实现模糊效果

          本文链接:https://www.haomeiwen.com/subject/oepxuttx.html