目前在我的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和URLS加载这样的数据:
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)
您可以做的是分派数据请求,然后调用动画:
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];
}
答案 1 :(得分: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 Task:
//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];
});
});
答案 2 :(得分:0)
此代码可能会使您的应用从应用商店中被拒绝,因为它会强制用户在每次启动应用时等待六秒钟。 Apple的指导方针表示,应用程序的发布应该尽快发布。
你能不能重构它,这样你的初始视图控制器可以在没有数据的情况下显示,然后在数据可用时逐步更新它?
如果从网络加载数据,您也可以对其进行缓存,以便后续启动更快。