iOS开发者视图控件iOS学习专题

iOS双击Home键,毛玻璃效果

2018-07-17  本文已影响268人  江湖闹士

项目中有时为了安全起见,在双击Home键,退到不活跃状态时,需要添加模糊效果,比如下图:

高斯模糊

要实现这种效果,非常简单,只需在APPdelegate里面添加几句代码即可

//AppDelegate.h中添加以下属性

@property (nonatomic,strong) UIVisualEffectView *visualEffectView;//毛玻璃效果
AppDelegate.m中

- (void)applicationWillResignActive:(UIApplication *)application {
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    self.visualEffectView.alpha = 0;
    self.visualEffectView.frame = self.window.frame;
    [self.window addSubview:self.visualEffectView];
    [UIView animateWithDuration:0.2 animations:^{
        self.visualEffectView.alpha = 1;
    }];

}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [UIView animateWithDuration:0.2 animations:^{
        self.visualEffectView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.visualEffectView removeFromSuperview];
    }];

}

以上就是实现毛玻璃的全部代码,是不是很简单呢~~~

后续加上:经测试这种写法有点小问题,就是如果项目中有拨打电话的操作(系统弹出拨打电话的提示框),这时弹出框下面的整个屏幕也是毛玻璃效果,这并不是我们想看到的,那怎么改呢,其实就是方法写的位置不对,按照下面的方法写

注意:将- (void)applicationWillResignActive:(UIApplication *)application;里面的代码放进- (void)applicationDidEnterBackground:(UIApplication *)application;里面,两个方法里面的代码虽然相似,但是属性值有所改变,要注意!!!

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    self.visualEffectView.alpha = 1;
    self.visualEffectView.frame = self.window.frame;
    [self.window addSubview:self.visualEffectView];
    [UIView animateWithDuration:0.2 animations:^{
        self.visualEffectView.alpha = 1;
    }];

}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [UIView animateWithDuration:0.2 animations:^{
        self.visualEffectView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.visualEffectView removeFromSuperview];
    }];

}

改成下面的方法后,上面出现的问题就解决了,只是可能首次进入后台的时候不能正常显示效果,支付宝也存在这种情况,但是基本上无伤大雅~~~

上一篇下一篇

猜你喜欢

热点阅读