退入后台的模糊效果(金融类软件和需要保密的软件需要此功能)

2019PHP高薪工程师学习路线图....>>>

此段代码放在appdelegate.m中,覆盖掉原来的空方法就可以了。

@interface AppDelegate ()
@property(assign, nonatomic) int index;
@property(strong, nonatomic) NSTimer *timer;
@end
#pragma mark - 退入后台的模糊效果
- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
                   ^{
                       self.index = 0;
                       if (self.timer) {
                           [self.timer invalidate];
                           self.timer = nil;
                       }
                       self.timer =
                       [NSTimer timerWithTimeInterval:1
                                               target:self
                                             selector:@selector(timerAction)
                                             userInfo:nil
                                              repeats:YES];
                       [[NSRunLoop currentRunLoop] addTimer:self.timer
                                                    forMode:NSDefaultRunLoopMode];
                       [[NSRunLoop currentRunLoop] run];
                   });
    
  UIImage *image = [self catchLocalScreen];
  //后台的模糊效果实现,当app进入后台时在app的上加一层覆盖视图
  UIImageView *bgImageView  = [[UIImageView alloc]
      initWithFrame:CGRectMake(0, 0, kSCreenWidth, kSCreenHeight)];
  bgImageView.image         = image;
  [bgImageView enableBlur:YES];
  bgImageView.blurTintColor = [UIColor colorWithWhite:1 alpha:0.3];
  bgImageView.blurStyle     = UIViewBlurDarkStyle;

  bgImageView.tag           = FrostedGlassTag;
  [[[UIApplication sharedApplication] keyWindow] addSubview:bgImageView];

  //设置app图标小圆点
  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
#pragma mark - 程序进入前台时移除遮罩视图
- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    [self.timer invalidate];
  //当app进入活动时,移除覆盖的模糊视图
  NSArray *array = [[UIApplication sharedApplication] keyWindow].subviews;

  for (id view in array) {
    if ([view isKindOfClass:[UIImageView class]]) {
      UIImageView *myView = view;
      if (myView.tag == FrostedGlassTag) {
        [myView removeFromSuperview];
      }
    }
  }

  //设置app图标小圆点
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];

}
- (void)timerAction{
    
    self.index++;
    NSLog(@"%d",_index);
    if (self.index >=10) {
        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            HJBRootViewController *tabBarC = [HJBRootViewController sharedManager];
//            self.window.rootViewController = tabBarC;
//        });

        [self.timer invalidate];
    }
}
//毛玻璃tag值
#define FrostedGlassTag 1008611
//屏幕宽高宏定义
#define kSCreenWidth [UIScreen mainScreen].bounds.size.width
#define kSCreenHeight [UIScreen mainScreen].bounds.size.height