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

时间:2011-07-27 18:05:40

标签: objective-c xcode cocoa xcode4

我正在尝试在启动期间添加活动指示器。我确实有一个发布图像,但我宁愿只有一个指标。我将以下内容添加到我的应用代理中,但指标未出现。

- (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:Activity Indicator during Startup?

4 个答案:

答案 0 :(得分:5)

对于所有需要这个的人,我知道有很多...... (在Xcode版本4.2.1上制作)

所以......

在AppDelegate.h中添加:

@property (nonatomic, strong) UIImageView *splashView;

在AppDelegate.m中添加:

在cours页面的顶部@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 setAnimationDelay:2.5] 负责根据您选择的延迟时间将splashView放在前面多长时间。

您可以通过更改x / y中的nubmers来改变指标的位置:
activityIndi​​cator.center = CGPointMake(160,360);

最后,根据方法:

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

只需添加:

[self performSelector:@selector(splashFade) withObject:nil];

然后你去:) 希望它有所帮助。

有一个很好的编程......

答案 1 :(得分:2)

我不认为通过查看UIApplicationDelegate

的文档可以做到这一点

您的应用程序最早被调用

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

顾名思义就是您的应用hasFinishedLaunching

显示和隐藏指示符时没有任何反应的原因与线程有关。用户界面只会在您的方法结束时更新,其中您的更改的最终结果将是指标被隐藏。

答案 2 :(得分:1)

另一个解决方案是让 launchimage 加载,当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];
}
  • 要停止动画和removeFromSuperView,您可能需要制作 baseView 指标属性+ @synthesize

答案 3 :(得分:0)

这是不可能的(如@ Paul.S所说)。但是,您可以通过向项目中添加(静态,不是动画)Default.png - 启动画面来模拟这一点。

在我看来,你应该投资于更好的表现。例如:您是否将应用程序与未使用的框架(例如QuartzCore,MapKit)或其他东西链接起来 启动应用程序最多需要2秒钟。在这段时间内,您实际上不需要向用户显示活动指标。如果确实需要更长的时间,你应该看看是什么让它变得如此缓慢,而不是试图隐藏它。