xcode 如何在我的 iOS 应用中添加 UIActivityIndi​​cator 视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14477253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:35:48  来源:igfitidea点击:

How to add a UIActivityIndicator View in my iOS app?

iosxcodemultithreadingloadinguiactivityindicatorview

提问by Zindokar

I don't know how to explain but... If you understand me when I say a "loading circle" perfect, I just want to do this

我不知道怎么解释但是...如果你理解我说的“加载圈”完美,我只是想这样做

    // 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

Probabbly I have to add a thread or something but I don't know how to do what I want exactly I hope you can help me, again. Thanks.

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

EDIT:

编辑:

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

Two things

两件事情

 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"

Thanks =)

谢谢=)

回答by Midhun MP

It can be done using MBProgressHUD

可以使用MBProgressHUD完成

Also check this code:

还要检查此代码:

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

});

EDIT:

编辑:

Never call methods like:

永远不要调用如下方法:

activityIndicator.startAnimating;
activityIndicator.stopAnimating;

These are used for calling setters and getters.

它们用于调用 setter 和 getter。

Change it to:

将其更改为:

[activityIndicator startAnimating];
[activityIndicator stopAnimating];

回答by Rob

You can use a UIActivityIndicatorView.

您可以使用 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];

Also, like shown by Midhun MP, you may have to use asynchronism to, for example, load data while your indicator is showing.

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

回答by D_D

You can alter the style of 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];

use any of these styles.

使用这些样式中的任何一种。

And This link can help to solve your second issue xCode "Property access results unused - getters should not be used for side effects"

并且此链接可以帮助解决您的第二个问题 xCode“未使用的属性访问结果 - 不应将吸气剂用于副作用”

SOLUTION: instead of using activityIndicator.startAnimating;and activityIndicator.stopAnimatinguse [activityIndicator startAnimating];and [activityIndicator stopAnimating];

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

回答by icodes

May be you can try Activity Indicator.

也许你可以试试活动指示器。

回答by lakesh

You can try using svprogresshud

您可以尝试使用svprogresshud

回答by MLBDG

For changing the size of the activity indicator:

要更改活动指示器的大小:

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

By the way, if you are populating a tableview, you may consider enabling the user interaction at the same time you launch the activity indicator:

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

[activityIndicator startAnimating];
tableView.userInteractionEnabled = NO;

and restoring it after:

并在之后恢复它:

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