【问题标题】:Delay with applicationDidEnterBackground延迟 applicationDidEnterBackground
【发布时间】:2014-04-25 13:49:25
【问题描述】:

我创建了第二个启动画面,它会在延迟后淡出。

我的实现应该可以正常工作,但似乎我在操作系统中发现了一个缺陷。打开应用程序:

  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];
        }
    }];
}

有什么想法可以解决这个问题吗?

【问题讨论】:

  • 您不应该为启动画面使用视图控制器而不是自己操作视图层次结构吗?
  • 不管怎样。延迟仍然存在。

标签: ios objective-c ios7 delay splash-screen


【解决方案1】:

在我看来,如果您希望您的应用程序显示您的启动图像,您显示启动图像的代码将在应用程序委托中的 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]];

}

【讨论】:

  • 我添加了新行。添加此新行后,请尝试一下。 :D
  • 这导致 EAGLContextView 的 renderbufferstorage:fromDrawable: 在 iOS 8 上从后台模式返回时失败,对此有何想法?
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 2019-06-07
  • 2015-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多