Splash screen on resume in Iphone

前端 未结 2 1884
长情又很酷
长情又很酷 2021-01-07 10:55

I want to add splash screen on my app when the app is resumed from the background.Is this possible? Thanks in advance

相关标签:
2条回答
  • 2021-01-07 11:44

    You can update your view stack in -[UIApplicationDelegate applicationWillResignActive:].

    The changes will be visible when the app resumes, and you can remove the splash screen again in -[UIApplicationDelegate applicationDidBecomeActive:].

    0 讨论(0)
  • 2021-01-07 11:48

    Some code to go along with Morten's answer. I also want to note that this does not behave properly in the simulator, but does when you run it on the device. The simulator shows a black screen until removeFromSuperview is called.

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // We don't want to show a splash screen if the application is in UIApplicationStateInactive (lock/power button press)
        if (application.applicationState == UIApplicationStateBackground) {
            UIImageView *splash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"splashimage.png"]];
            splash.frame = self.window.bounds;
            [self.window addSubview:splash];
        }
    
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Make sure you do not remove the last view in the event something odd happens
        if ([[self.window subviews] count] > 1) {
            // Not recommended by Apple, but client gets what client wants
            [NSThread sleepForTimeInterval:1.0];
            [[[self.window subviews] lastObject] removeFromSuperview];
        }
    }
    

    Note, I used applicationDidEnterBackground instead of applicationWillResignActive because applicationState helps to differentiate between "pressed home button" and "pressed lock/power button". UIApplicationStateBackground = "pressed home button".

    0 讨论(0)
提交回复
热议问题