如何在我的iOS应用程序中添加UIActivityIndi​​cator View?

时间:2013-01-23 10:18:50

标签: ios xcode multithreading loading uiactivityindicatorview

我不知道如何解释但是...如果你在我说“装载圈”完美时理解我,我只想这样做

    // Start loading in the middle of the screen frozing all interaction
    for (int c = 0; c < ([barcos count] - 1); c++)
    {
        NSArray *datos = [[barcos objectAtIndex:c] componentsSeparatedByString:@";"];
        NSString *nombreImagen = [datos objectAtIndex:2];
        NSURL *accesoFtp = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",urlFtp,nombreImagen]];
        NSData *imagen = [NSData dataWithContentsOfURL:accesoFtp];
        [imagen writeToFile:[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Barcos/%@",nombreImagen]]] atomically:NO];
    }
    // Stop frozing all interaction and remove the loading circle

可能我必须添加一个线程或其他东西,但我不知道如何做我想要的东西我希望你能再次帮助我。感谢。

编辑:

UIActivityIndicatorView *activityIndicator;
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];

activityIndicator.startAnimating;

dispatch_queue_t queue = dispatch_get_global_queue(0,0);

dispatch_async(queue, ^{

        for (int c = 0; c < ([barcos count] - 1); c++)
        {
            NSArray *datos = [[barcos objectAtIndex:c] componentsSeparatedByString:@";"];
            NSString *nombreImagen = [datos objectAtIndex:2];
            NSURL *accesoFtp = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",urlFtp,nombreImagen]];
            NSData *imagen = [NSData dataWithContentsOfURL:accesoFtp];
        [imagen writeToFile:[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Barcos/%@",nombreImagen]]] atomically:NO];
        }

    dispatch_sync(dispatch_get_main_queue(), ^{
        activityIndicator.stopAnimating;
    });
});

两件事

 1.- The activity indicator is too small but works, if I can to it bigger or same size but make darker the background would be better (Thanks!)
 2.- I have a warning with startAnimating and stopAnimating "Property access result unused - getters should not be used for side effects"

谢谢=)

6 个答案:

答案 0 :(得分:4)

可以使用MBProgressHUD

完成

同时检查此代码:

//Show your activity indicator here

dispatch_queue_t queue = dispatch_get_global_queue(0,0);

dispatch_async(queue, ^{
    for (int c = 0; c < ([barcos count] - 1); c++)
    {
        NSArray *datos = [[barcos objectAtIndex:c] componentsSeparatedByString:@";"];
        NSString *nombreImagen = [datos objectAtIndex:2];
        NSURL *accesoFtp = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",urlFtp,nombreImagen]];
        NSData *imagen = [NSData dataWithContentsOfURL:accesoFtp];
        [imagen writeToFile:[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Barcos/%@",nombreImagen]]] atomically:NO];
    }
    dispatch_sync(dispatch_get_main_queue(), ^{
              //hide that activity indicator here
            });

});

修改

永远不要调用以下方法:

activityIndicator.startAnimating;
activityIndicator.stopAnimating;

这些用于调用setter和getter。

将其更改为:

[activityIndicator startAnimating];
[activityIndicator stopAnimating];

答案 1 :(得分:3)

您可以使用UIActivityIndi​​catorView。

UIActivityIndicatorView *activityIndicator;

activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];

此外,如Midhun MP所示,您可能必须使用异步,例如,在指示器显示时加载数据。

答案 2 :(得分:1)

可能您可以尝试活动指标。

答案 3 :(得分:1)

您可以尝试使用svprogresshud

答案 4 :(得分:1)

您可以更改activityindicator的样式,

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActionSheetStyleDefault];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActionSheetStyleBlackOpaque];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActionSheetStyleBlackTranslucent];

使用任何这些样式。

此链接可以帮助您解决第二个问题 xCode "Property access results unused - getters should not be used for side effects"

解: 而不是使用activityIndicator.startAnimating;activityIndicator.stopAnimating 使用[activityIndicator startAnimating];[activityIndicator stopAnimating];

答案 5 :(得分:0)

用于更改活动指示符的大小:

activityIndicator.frame = CGRectMake(0.0, 0.0, 80.0, 80.0);
activityIndicator.transform = CGAffineTransformMakeScale(1.75, 1.75);

顺便说一句,如果您正在填充tableview,您可以考虑在启动活动指示符的同时启用用户交互:

[activityIndicator startAnimating];
tableView.userInteractionEnabled = NO;

并在以下后恢复:

[self.tableView reloadData];
[activityIndicator stopAnimating];
tableView.userInteractionEnabled = YES;