模糊效果在iOS 11中停止工作

时间:2017-11-19 17:02:23

标签: ios ios11 uivisualeffectview uiblureffect

我使用以下代码创建了blur effect,它在iOS 10上工作正常,但它已停止在iOS 11上工作。我无法查看blur当应用移至background时。当应用进入foreground时,blur stays always和应用不再有效,只有relaunching才能使其正常工作应用程序。

iOS 11是否有解决此问题的方法?

-(void)addBlurEffect{
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    blurEffectView.frame = window.frame;
    blurEffectView.tag = 4444;
    [window addSubview:blurEffectView];

}

-(void)removeBlurEffect{
    NSArray *allWindows = [[UIApplication sharedApplication] windows];
    for (UIWindow * aWindow in allWindows) {
        UIView *blurEffectView = [aWindow viewWithTag:4444];
        if (blurEffectView){
            [blurEffectView removeFromSuperview];
        }
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self addBlurEffect];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self removeBlurEffect];
}

1 个答案:

答案 0 :(得分:0)

这是因为您在整个时间内在WRONG委托中执行此操作。您需要阅读AppDelegate中有关每种方法的文档或注释。

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

    [self addBlurEffect];
}

- (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.

    [self removeBlurEffect];
}

因此,请将上述两种方法视为“暂停”或“取消暂停”游戏。当游戏进入后台或被中断时,通常会将其暂停给用户,这样他们就不会失去进度或死亡或当它处于后台状态或转换状态时(IE:任务管理器状态)。

然后当他们恢复并且应用程序在屏幕上时,您取消暂停。这将需要刷新和无效的图形..以上两种方法处理..

但是,你是这样做的:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

此方法用于保存状态和清理..不修改UI .. IE:将数据存储在数据库中或将用户设置同步到磁盘或某物或释放内存..用户可能会在此状态下杀死应用程序或系统也可以杀死它。

只有当应用程序实际最小化时才会被调用(最小化/背景状态与任务切换器状态/处于活动状态不同!)。iOS 11有不同的转换,所以现在我们实际上看到了差异我想..

如果将代码移动到活动状态处理程序,它可以正常工作..