iOS app切换后台时添加模糊遮罩层

2023-12-13 03:34:38

仿 支付宝 退出后台后,App整个 增加模糊遮罩层

此处只介绍 在iOS13后?SceneDelegate 下的操作

原理就是

在?App 进入后台后 在 主window上添加一个 ?UIVisualEffectView

在进入前台后移除

直接上代码:

先声明:

//先声明
/* blurView */
@property (strong, nonatomic) UIVisualEffectView *blurView;

在代理方法中:

- (void)sceneDidBecomeActive:(UIScene *)scene;

- (void)sceneWillResignActive:(UIScene *)scene;

- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    if (_blurView) {
        [_blurView removeFromSuperview];
    }
}

- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
    if(!_blurView) {
           UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
           _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
           _blurView.frame = self.window.bounds;
       }
    //进入后台实现模糊效果
    [self.window addSubview:_blurView];
}

收工,

如果没有SceneDelegate ,只有AppDelegate

同理在

- (void)applicationDidBecomeActive:(UIApplication *)application;

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

添加相对应的代码即可.

文章来源:https://blog.csdn.net/KLong27/article/details/134883522
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。