0

我正在尝试使用 MBProgressHUD 在应用程序加载更多 json 数据时弹出加载动画。这是我用来调用包含 MBProgressHUD 的函数的代码。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height) {
    pageCounter++;


    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // Do something...
        [self loadAdditionalJson];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });

    }
}

这是被调用的函数:

-(void)loadAdditionalJson

{

NSLog(@"%i", pageCounter);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
                                   @"http://ragetracks.com/?json=1&count=50&page=%i&custom_fields=PostThumb&include=title,content,attachments",
                                   pageCounter]];


NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    for (int x = 0; x < 50; x++) {
        //NSLog(@"%i", pageCounter*50 -50 + x);
        if ([JSON[@"posts"][x][@"attachments"] count] == 0)
        {
            NSString *temp = [NSURL URLWithString:@"nothing"];
            [imageUrls addObject:temp];
        }
        else
        {
            NSString *temp1;
            //temp1 = [NSString stringWithFormat:@"%@", JSON[@"posts"][x][@"id"]];
            //NSLog(temp1);
            NSURL *imageUrl =  [NSURL URLWithString:
                                [NSString stringWithFormat:@"%@",
                                JSON[@"posts"][x][@"attachments"][0][@"images"][@"medium"][@"url"]]];
           NSString *title = [NSString stringWithFormat:@"%@", JSON[@"posts"][x][@"title"]];

            NSString *label = [NSString stringWithFormat:@"%@", JSON[@"posts"][x][@"content"]];

            NSScanner *scanner = [NSScanner scannerWithString:label];
            [scanner scanUpToString:@"%2Ftracks%2F" intoString:nil];
            [scanner scanString:@"%2Ftracks%2F" intoString:nil];
            [scanner scanUpToString:@"&" intoString:&temp1];

            NSURL *temp2 = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.soundcloud.com/tracks/%@.json?client_id=7622aa84a50c9f7609e2f7ed8bc85e81", temp1]];

            if (imageUrl) {
                [imageUrls addObject:imageUrl];
            }
            else{
                NSString *temp = [NSURL URLWithString:@"nothing"];
                [imageUrls addObject:temp];
            }
            [titles addObject:title];
            [contentUrls addObject:temp2];

        }
    }
    [self.collectionView reloadData];

} failure:nil];


    [operation start];
}

加载动画只是出现和消失太快,并不能反映应用程序加载新信息需要多长时间。我知道它与主线程有关,但我不确定该怎么做。我试过关闭,AFNetworkActivityIndicatorManager但这似乎没有任何影响。睡眠系统会延迟它,但仍然不能正确反映加载时间,因为睡眠后,它会回来并完成加载。

4

1 回答 1

1

好吧,您要求它...您在开始 JSON 加载之前延迟了很短的时间(这似乎毫无意义),然后在开始加载后立即关闭进度指示器。

我认为您的困惑是您认为 JSON 加载是同步完成的 - 但事实并非如此。

所以,删除:

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

因为它对你没有帮助。只需在启动 HUD 动画后开始加载。然后,在处理 JSON 的成功块 (on JSONRequestOperationWithRequest) 中,在扫描代码之后,添加:

[MBProgressHUD hideHUDForView:self.view animated:YES];

如果应该马上去[self.collectionView reloadData];

于 2013-06-09T21:16:26.287 回答