题
我试图在我的 iPhone 应用程序下载网络资源时显示加载图标,但我不知道如何使其正确显示。
我四处搜寻,发现了一些细节 UIActivityView
类,但可用的示例源代码不起作用,并且文档有点简洁。
有人可以提供一个关于如何使用此类的简单示例吗?
解决方案
假设您已经设置了一个视图控制器,并且想要添加一个 UIActivityIndicator
对此,你可以这样做:
(假设你有一个名为的成员变量 indicator
, ,您可以稍后使用它来清理)
对于您的接口(.h 文件):
UIActivityIndicator *indicator;
对于您的实施(.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];
停止动画
[indicator removeFromSuperview];
indicator = nil;
其他提示
Ben的回答与我正在做的非常相似 - 你对线程的猜测可能是准确的。您使用 NSURLConnection
来处理下载吗?如果是这样,您使用的是同步还是异步版本?如果它是同步版本并且您只是在同步调用周围启动和停止动画,那么在您停止动画之后UI才会更新。
不隶属于 StackOverflow