【问题标题】:Blur effect stopped working in iOS 11模糊效果在 iOS 11 中停止工作
【发布时间】:2018-05-02 22:25:24
【问题描述】:

我使用以下代码创建了blur effect,它在iOS 10 上运行良好,但在iOS 11 上停止运行。当应用程序移动到background 时,我无法查看blur。当应用程序来到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];
}

【问题讨论】:

  • 应用在后台添加模糊效果有什么意义?该应用程序未在后台运行;没什么可看的。这段代码想做什么?
  • 如果出于安全考虑,您希望您的应用在后台不显示任何内容,那么您可以在顶部添加一个视图来隐藏您的应用屏幕。

标签: ios ios11 uivisualeffectview uiblureffect


【解决方案1】:

发生这种情况是因为您一直在错误的代表中执行此操作。您需要阅读AppDelegate 中的文档或 cmets,了解每种方法的作用。

- (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 有不同的转换,所以现在我们实际上看到了不同之处猜猜……

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

【讨论】:

  • 正如你所说,iOS 11 有不同的过渡风格,有时模糊效果没有添加。知道为什么双击设备主页按钮时不会调用这些委托方法吗?
猜你喜欢
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2014-01-30
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多