【问题标题】:Display a loading icon while a network resource is being downloaded在下载网络资源时显示加载图标
【发布时间】:2010-09-17 18:05:04
【问题描述】:

我试图在我的 iPhone 应用下载网络资源时显示加载图标,但我不知道如何让它正确显示。

我四处搜索,发现了一些关于 UIActivityView 类的详细信息,但可用的示例源代码不起作用,而且文档有点简洁。

谁能提供一个简单的例子来说明如何使用这个类?

【问题讨论】:

    标签: iphone ios networking


    【解决方案1】:

    假设您已经设置了一个视图控制器,并且想在其中添加一个 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;
    

    【讨论】:

      【解决方案2】:

      Ben 的回答看起来与我正在做的非常相似 - 您对线程的猜测可能是准确的。您是否使用NSURLConnection 来处理您的下载?如果是这样,您使用的是同步版本还是异步版本?如果它是同步版本,并且您只是在同步调用周围启动和停止动画,那么 UI 在您停止动画之前不会更新。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-26
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2014-01-28
        • 1970-01-01
        • 2012-12-06
        • 2018-06-08
        相关资源
        最近更新 更多