应用程序启动期间显示活动指示器

3

我想在启动时添加一个活动指示器。之前我有一张启动图片,但现在我更想要只有一个指示器。我在我的应用委托中添加了以下内容,但是指示器并没有出现。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // *******Create activity indicator****
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(225, 115, 30, 30)];
    [activity setBackgroundColor:[UIColor clearColor]];
    [activity setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleGray];
    [window addSubview: activity];
    [activity release];

    [activity startAnimating];
    activity.hidden = FALSE;
    // *******End activity indicator****

    MainViewController *viewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    self.mainViewController = viewController;

    [window addSubview:[mainViewController view]];
    mainViewController.view.frame = CGRectMake(0, 20, 320, 411);

    [window addSubview:[rootController view]];
    [window makeKeyAndVisible];
#if !TARGET_IPHONE_SIMULATOR
    [application registerForRemoteNotificationTypes: 
     UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
#endif

    application.applicationIconBadgeNumber = 0;

    // Hide indicator
    [viewController release];
    activity.hidden = TRUE;

    [activity stopAnimating];
    return YES;
}

我查看了这个2009年的问题,但是解决方案对我不起作用。 iPhone-SDK:启动期间显示活动指示器?

4个回答

5

对于那些需要此功能的人,我知道有很多...

(使用Xcode版本4.2.1创建)

所以...

在AppDelegate.h文件中添加以下内容:

@property (nonatomic, strong) UIImageView *splashView;

在AppDelegate.m中加入以下内容:

在页面顶部添加 @synthesize splashView;
然后:

- (void) splashFade
{
    splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
    splashView.image = [UIImage imageNamed:@"Default.png"];
    [_window addSubview:splashView];
    [_window bringSubviewToFront:splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationDelay:2.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:_window cache:YES];
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.alpha = 0.0;
    [UIView commitAnimations];

    //Create and add the Activity Indicator to splashView
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.alpha = 1.0;
    activityIndicator.center = CGPointMake(160, 360);
    activityIndicator.hidesWhenStopped = NO;
    [splashView addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [splashView removeFromSuperview];
}
[UIView animateWithDuration:1.0 animations:^{...}], the duration of the animation is set to 1.0 second by default, and you can change it to any value you want.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

只需添加以下内容:
[self performSelector:@selector(splashFade) withObject:nil];

好的,马上为您翻译。

完成啦 :)
希望能对您有所帮助。

祝您编程愉快...


如何在 Xcode 11.4 版本中使用上述解决方案? - Ahmed Saeed

4

从阅读UIApplicationDelegate文档来看,我认为这是不可能的。

你的应用程序被调用的最早时间是在

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

“hasFinishedLaunching”是指应用程序已经完成启动的状态。

当您展示和隐藏指示器时没有任何反应,这是由于线程问题。在您的方法结束时,用户界面将仅更新变更的最终结果,此时指示器将变为隐藏状态。


1
另一种解决方案是让启动图片加载,当您的ViewController在viewDidLoad时加载时,在视图顶部使用叠加+指示器,并使alpha小于1:
UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = NO;
baseView.alpha = 0.4;
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = self.view.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];

现在是有趣的部分,使用委托来移除指示器+覆盖层,当所有后台线程完成加载时,通过您的委托调用一个方法即可。
- (void) onFinishLoading{
        [indicator stopAnimating];
        [baseView removeFromSuperview];
}

为了停止动画并从父视图中移除,您可能需要使baseViewindicator属性+@synthesize。

0

这是不可能的(正如@Paul.S所说)。但是,您可以通过向项目添加(静态的,因此不会动画化)Default.png启动屏幕来模拟此操作。

在我看来,您应该更注重性能。例如:您是否将应用程序与未使用的框架(例如QuartzCore、MapKit)链接起来了。
启动应用程序最多应该需要2秒钟。在此期间,您不需要向用户显示活动指示器。如果需要更长时间,请查看是什么使其变得如此缓慢,而不是试图隐藏它。


不,我尽可能保持框架的简洁。加载时间大约为2秒或更短。我的老板只是想要一个指示器和美观性,所以如果无法实现也不重要。 - Tanoro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接