【问题标题】:iPhone-SDK:Activity Indicator during Startup?iPhone-SDK:启动期间的活动指示器?
【发布时间】:2009-09-16 19:27:29
【问题描述】:

我的项目中有一个 Default.png 文件,因此每当我的应用程序启动时 Default.png 文件就会加载到那里。由于我的应用程序在启动(应用程序启动)时从服务器检索数据,我想在加载 Default.png 文件时也显示活动指示器。是否可以一次激活两个进程,我的意思是在启动期间一次显示 Default.png 和启用活动指示器? 但我尝试将活动指示器代码放在“applicationDidFinishLaunching”中,但在显示 Default.png 文件时它不显示活动指示器。但我确信我拥有的活动代码在其他屏幕上运行良好。问题仅出现在应用启动期间。

有人可以分享你的想法吗?

谢谢。

分离/

【问题讨论】:

标签: iphone


【解决方案1】:

你不能在 Default.png 图像中做任何动画。您应该尽快用视图/控制器(包含活动指示器)替换它。在显示该控制器之后,加载您的数据以及可能的其他控制器(在一个线程中)。

【讨论】:

    【解决方案2】:

    我在我的应用程序中所做的是立即加载一个带有覆盖屏幕的 Default.png 图像的图像视图。然后你可以在图像视图的顶部添加一个进度指示器,同时你的服务器在后台线程上工作。

    您应该始终避免在 applicationDidLaunch 方法中进行网络活动,因为此方法有一个超时时间,如果它没有返回,Apple 将终止您的应用程序。这很重要,因为假设用户处于边缘状态并且服务器行程需要 30 秒 - 您的应用似乎已经崩溃给用户。

    通过在后台线程中进行网络工作并返回 applicationDidLaunch,您将防止苹果杀死您的应用,您将能够让用户在新数据进入之前使用该应用(如果您愿意)和您的应用似乎启动速度更快。

    【讨论】:

      【解决方案3】:

      您应该始终避免在 applicationDidLaunch 方法中进行网络活动,因为此方法有一个超时时间,如果它没有返回,Apple 将终止您的应用程序。这很重要,因为假设用户处于边缘状态并且服务器行程需要 30 秒 - 您的应用似乎已经崩溃给用户。

      所以你认为我不应该把 XMLPraser 放在 applicationDidLaunch 上?

      NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
      NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
      
      //Initialize the delegate.
      XMLParser *parser = [[XMLParser alloc] initXMLParser];
      
      //Set delegate
      [xmlParser setDelegate:parser];
      
      //Start parsing the XML file.
      BOOL success = [xmlParser parse];
      
      if(success)
          NSLog(@"No Errors");
      else
          NSLog(@"Error Error Error!!!");
      

      【讨论】:

        【解决方案4】:

        这就是它的工作原理:
        (基于 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 setAnimationDelay:2.5] 负责根据您选择的延迟时间,splashView 将在前面多长时间。

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

        最后,在方法下:

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

        只需添加这个:

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

        你去吧:)
        希望对您有所帮助。

        祝你编程愉快......

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-26
          • 1970-01-01
          相关资源
          最近更新 更多