2014-05-20 56 views
0

目前在我AppDelegate.m我的代码:播放动画时的数据加载

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [self.window makeKeyAndVisible]; 
    CGRect rect = [[UIScreen mainScreen] bounds]; 
    UIImageView *imageView =[[UIImageView alloc] initWithFrame:rect]; 
    imageView.animationImages = [NSArray arrayWithObjects: 
           [UIImage imageNamed:@"Comp 2_00000.png"],..., nil]; 
    imageView.animationDuration = 6; 
    imageView.animationRepeatCount = 1; 
    [imageView startAnimating]; 
    [self.window addSubview:imageView]; 

    return YES; 
} 

起着动画我有一次我的观点没有在我ViewController.m加载代码加载一些数据需要大约5秒。

我加载数据这样的使用JSON和URL:

NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.co.uk/untitled%20folder/jsonimages.php"]; 
    NSData *jsonData = [NSData dataWithContentsOfURL:url]; 
    NSError *error = nil; 

    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData 
                  options:NSJSONReadingMutableContainers error:&error]; 

反正是有对数据加载时要播放的动画?

+0

为什么不可以编辑您的问题,以显示你如何加载你的数据? –

回答

1

创建一个辅助功能(使静态的,如果您需要重复使用):

功能:

-(UIImageView *) InitImageView{  
    [self.window makeKeyAndVisible]; 
    CGRect rect = [[UIScreen mainScreen] bounds]; 
    UIImageView *imageView =[[UIImageView alloc] initWithFrame:rect]; 
    imageView.animationImages = [NSArray arrayWithObjects: 
          [UIImage imageNamed:@"Comp 2_00000.png"],..., nil]; 
    //imageView.animationDuration = 6; 
    imageView.animationRepeatCount = 1; 

    [self.view addSubview:imageView]; 

    return imageView; 
} 

Ansynchronous任务:

//Start Animation 
UIImageView *imageView = [self InitImageView]; 
[imageView startAnimating]; 

dispatch_queue_t queue = dispatch_queue_create("co.uk.bits.bots", NULL); 
dispatch_async(queue, ^{ 
NSURL *url = [NSURL URLWithString:@"http://xx.co.uk/jsonimages.php"]; 
    NSData *jsonData = [NSData dataWithContentsOfURL:url]; 
    NSError *error = nil; 

    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData 
         options:NSJSONReadingMutableContainers error:&error]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     //Stop Animation 
     [imageView stopAnimating]; 
    }); 
}); 
0

此代码可能会让您的应用在应用商店中被拒绝,因为它会强制用户在每次应用启动时等待6秒钟。苹果的指导方针表示应用程序的发布应该尽可能快。

您可以重构它,以便您的初始视图控制器可以在没有数据的情况下首先显示,然后在数据可用时递增更新它?

如果从网络加载数据,您也可以缓存它,以便后续启动速度更快。

+0

这是真的吗?因为我看到了一些应用程序,例如网络[EE](https://itunes.apple.com/gb/app/my-ee/id567457151?mt=8)的应用程序有一个启动动画。它只是因此使启动时间约10秒。我认为我最好还是先用动画来隐藏这个等待。甚至当我缓存图像来获得这个动画。只因为它很好。 – maxisme

+0

加载数据时启动动画时没有问题,但必须在'didFinishLaunchingWithOptions'方法之外执行动画。 'didFinishLaunchingWithOptions'方法必须快速返回,否则可能会被拒绝:但是您可以在'didFinishLaunchingWithOptions'中提供一个控制器,它将显示您的加载并尽快将焦点集中到应用程序。 – Emilie

1

你可以做的是调度数据请求,然后调用动画:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSURL *url = [NSURL URLWithString:@"http://xxxxxxxx.co.uk/untitled%20folder/jsonimages.php"]; 
    NSData *jsonData = [NSData dataWithContentsOfURL:url]; 
    NSError *error = nil; 

    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
       //Main thread stuff, after loading the data 
       [self animate]; 
     }); 
}); 

....

- (void)animate { 
    CGRect rect = [[UIScreen mainScreen] bounds]; 
    UIImageView *imageView =[[UIImageView alloc] initWithFrame:rect]; 
    imageView.animationImages = [NSArray arrayWithObjects: 
           [UIImage imageNamed:@"Comp 2_00000.png"],..., nil]; 
    imageView.animationDuration = 6; 
    imageView.animationRepeatCount = 1; 
    [imageView startAnimating]; 
    [self.view addSubview:imageView]; 
} 
+0

这是在ViewController.m? – maxisme

+0

是的,在你的ViewController中,这是做什么是下载数据在一个单独的线程,让主线程免费做你的动画;) – ivaN

+0

'[self.window addSubview:imageView];'给出错误 – maxisme