在下载网络资源时显示加载图标

8 iphone networking ios

我正在尝试在我的iPhone应用程序下载网络资源时显示加载图标,但我无法弄清楚如何使其正确显示.

我四处搜索并发现了UIActivityView类的一些细节,但可用的示例源代码不起作用,文档很简洁.

有人可以提供一个关于如何使用这个类的简单示例吗?

Ben*_*ieb 11

假设您已经设置了一个视图控制器,并且想要添加一个视图控制器UIActivityIndicator,那么您可以这样做:

(假设您有一个名为的成员变量indicator,稍后可以使用它来清理)

对于您的界面(.h文件):

UIActivityIndicator *indicator;
Run Code Online (Sandbox Code Playgroud)

对于您的实现(.m文件):

启动动画

CGRect b = self.view.bounds;
indicator = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle: 
                                             UIActivityIndicatorStyleWhite];
//center the indicator in the view
indicator.frame = CGRectMake((b.size.width - 20) / 2, (b.size.height - 20) / 2, 20, 20); 
[self.view addSubview: indicator];
[indicator release];
[indicator startAnimating];
Run Code Online (Sandbox Code Playgroud)

停止动画

[indicator removeFromSuperview];
indicator = nil;
Run Code Online (Sandbox Code Playgroud)