gpt4 book ai didi

ios - 当应用程序处于非事件状态时覆盖图像

转载 作者:可可西里 更新时间:2023-11-01 05:00:34 24 4
gpt4 key购买 nike

我正在使用这些代码来

1) 在应用程序处于非事件状态时覆盖图像(双击主页按钮)。

2) 在应用程序处于事件状态时删除图像(重新打开应用程序)

在 appdelegate.h 文件中:

@property (nonatomic, strong) UIImageView *splashScreenImageView;

在 appdelegate.m 文件中:

- (void)applicationWillResignActive:(UIApplication *)application
{
UIImage *splashScreenImage = [UIImage imageNamed:@"BackgroundScreenCaching"];
_splashScreenImageView = [[UIImageView alloc] initWithImage:splashScreenImage];
[self.window addSubview:_splashScreenImageView];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(_splashScreenImageView != nil) {
[_splashScreenImageView removeFromSuperview];
_splashScreenImageView = nil;
}
}

问题:

但是,SOMETIME 当按下主页按钮两次时,iOS 仍然缓存包含敏感信息的应用程序屏幕,而不是 iOS 11 中的叠加图像。在 iOS10 中测试没有问题。

已更新

改成这样后问题依旧:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

BOOL result = [super application:application didFinishLaunchingWithOptions:launchOptions];
...
UIImage *splashScreenImage = [UIImage imageNamed:@"BackgroundScreenCaching"];
_splashScreenImageView = [[UIImageView alloc] initWithImage:splashScreenImage];
[_splashScreenImageView setFrame:self.window.bounds];


return result;
}


- (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.
if(_splashScreenImageView != nil) {
[_splashScreenImageView removeFromSuperview];
}
}

- (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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
[self.window addSubview:_splashScreenImageView];
}

最佳答案

我发现了一个棘手的方法,它适用于 iOS 11 或 iOS 10。

在 Appdelegate.m 文件中:

 @implementation AppDelegate {
UIImageView *splashScreenImageView;
UIViewController *viewController;
}

在 didFinishLaunchingWithOptions 中添加此代码以设置图像和更新框架

    splashScreenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
splashScreenImageView.image = [UIImage imageNamed:@"BackgroundScreenCaching"];

实现这个方法来获取最顶层的 View Controller

- (UIViewController *)topViewController{
return [self topViewController:[UIApplicationsharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}

if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}

UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}

现在在 applicationWillResignActive 方法中首先获取顶层 viewController 并将该 View 上的 splashScreenImageView 设置为 subview

- (void)applicationWillResignActive:(UIApplication *)application {
viewController = [self topViewController];
[viewController.view addSubview:splashScreenImageView];
}

最后在 applicationDidBecomeActive 中,当应用第一次打开时移除覆盖图像并打开应用

- (void)applicationDidBecomeActive:(UIApplication *)application {
[splashScreenImageView removeFromSuperview];
}

这个技巧会奏效。

关于ios - 当应用程序处于非事件状态时覆盖图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com