美文网首页iOS 控件定制iOS Developer程序员
iOS实现从后台切换到前台有毛玻璃效果

iOS实现从后台切换到前台有毛玻璃效果

作者: 兰州一碗面 | 来源:发表于2017-03-10 15:37 被阅读466次

    先上图

    Untitled.gif Untitled1.gif
    ---------------------------------------------------------模拟器上效果不是很好-------------------------------------------------------

    思路:截取当前屏幕,添加毛玻璃效果,进入前台时显示图片,0.2秒后透明度为0并删除。

    在.h文件中
    + (void)addBlurryScreenImage;       //从后台进入前台添加模糊效果
    + (void)removeBlurryScreenImage;    //进入前台后去除模糊效果
    
    
    在.m文件中
    #pragma mark ----------- 当前屏幕截屏 -----------
    
    + (UIImage *)screenShot {
    
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        
        UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0);
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        for (UIWindow *window in [UIApplication sharedApplication].windows) {
    
              if ([window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
                
                CGContextSaveGState(context);
                
                CGContextTranslateCTM(context, window.center.x, window.center.y);
    
                CGContextConcatCTM(context, [window transform]);
                
                CGContextTranslateCTM(context, -[window bounds].size.width * [[window layer] anchorPoint].x, -[window bounds].size.height * [[window layer] anchorPoint].y);
                
                [[window layer] renderInContext:context];
    
                CGContextRestoreGState(context);
            }
            
        }
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return img;
    }
    
    
    在.m文件中
    //从后台进入前台添加模糊效果
    
    + (void)addBlurryScreenImage {
        
        UIImageView *imgView    = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        
        imgView.tag             = ScreenImgViewTag;
        
        imgView.image           = [[self screenShot] imgWithBlur]; //添加毛玻璃
        
        [[[UIApplication sharedApplication] keyWindow] addSubview:imgView];
        
    }
    
    //进入前台后去除模糊效果
    
    + (void)removeBlurryScreenImage {
        
        for (id object in [[UIApplication sharedApplication] keyWindow].subviews) {
            
            if ([[object class] isSubclassOfClass:[UIImageView class]]) {
            
                UIImageView *screenImgView = (UIImageView *)object;
                
                if (screenImgView.tag == ScreenImgViewTag) {
                    
                    [UIView animateWithDuration:0.2 animations:^{
                        screenImgView.alpha = 0;
                    } completion:^(BOOL finished) {
                        [screenImgView removeFromSuperview];
                    }];
                    
                }
            
            }
        }
        
    }
    
    在AppDelegate.m中
    
    - (void)applicationWillResignActive:(UIApplication *)application {
            //添加模糊截屏
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
            //去除模糊截屏
    }
    

    功能以实现。

    github链接

    相关文章

      网友评论

        本文标题:iOS实现从后台切换到前台有毛玻璃效果

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