gpt4 book ai didi

ios - 在加载数据时播放动画

转载 作者:行者123 更新时间:2023-11-28 22:06:41 26 4
gpt4 key购买 nike

目前在我的 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;
}

一旦我的 View 在我的 ViewController.m 中加载了代码,它就播放了一个动画。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];

加载数据时是否播放动画?

最佳答案

创建一个辅助函数(如果需要重用,则将其设为静态):

功能:

-(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;
}

异步任务:

//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];
});
});

关于ios - 在加载数据时播放动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23767130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com