簡體   English   中英

延遲applicationDidEnterBackground

[英]Delay with applicationDidEnterBackground

我創建了第二個啟動屏幕,它在延遲后消失了。

我的實現應該運行良好,但是似乎我發現了操作系統中的缺陷。 打開應用程序:

  1. 我按下主頁按鈕
  2. 等待一秒鍾以上
  3. 再次打開應用

然后它將適當顯示我的自定義啟動屏幕。 但是,如果我:

  1. 按下主屏幕按鈕
  2. 立即打開應用

然后顯示主視圖,大約0.5秒后,啟動屏幕出現在屏幕上,並且幾乎立即消失,這是不希望的效果。

因此,看來,如果我足夠快地返回到應用程序,那么它仍然會卡在applicationWillResignActive中,然后在應用程序重新出現時繼續前進到applicationWillEnterForeground。

代碼如下:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NJALoginViewController *loginViewController = [[NJALoginViewController alloc] init];

    self.splashView = [[NJASplashView alloc] initWithFrame:self.window.frame];

    self.window.rootViewController = loginViewController;
    [self.window makeKeyAndVisible];

    [self.window addSubview:self.splashView];
    [self performSelector:@selector(removeSplashScreen) withObject:nil afterDelay:0.5];

    return YES;
}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.splashView setAlpha:1];
    [self.window addSubview:self.splashView];
    [self.window bringSubviewToFront:self.splashView];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [UIView animateWithDuration:.5 delay:.7 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.splashView setAlpha:0];
    } completion:^(BOOL finished) {
        if (finished) {
            [self.splashView removeFromSuperview];
        }
    }];
}

- (void)removeSplashScreen
{
    [UIView animateWithDuration:.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.splashView setAlpha:0];
    } completion:^(BOOL finished) {
        if (finished) {
            [self.splashView removeFromSuperview];
        }
    }];
}

有什么想法我可以解決這個問題嗎?

在我看來,如果您希望應用程序顯示初始圖像,則顯示初始圖像的代碼將在應用程序委托的applicationWillResignActive中。 因為您不能在后台處理UI組件。 因此,在進入后台之前,您必須處理UI組件。

像這樣:

- (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.splashView setAlpha:1];
    [self.window addSubview:self.splashView];
    [self.window bringSubviewToFront:self.splashView];

    // add new code
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM