1

我想在某些时候显示进度条,但进度条没有改变。我该怎么办?

dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

dispatch_group_async(group, queue, ^{
    for (int i = 0; i < 100; i++)
    {
        dispatch_group_async(group, dispatch_get_main_queue(),^{
            self.progressBar.progress = (float)i/100;
        });

        // do big somethings
    }
});
4

3 回答 3

1

在您最初的问题中,您设置progress为 0 到 100 之间的整数。如果这是 a UIProgressView,那么progress是一个浮点数,应该从 0 到 1.0。

dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

dispatch_group_async(group, queue, ^{
    for (int i = 0; i < 100; i++)
    {
        dispatch_group_async(group, dispatch_get_main_queue(),^{
            self.progressBar.progress = ((CGFloat) i) / 100.0;
        });

        // do big somethings
    }
});

或者,更简单地说,因为您并没有真正使用组(您在全局队列上只有一个操作,并且在处理串行队列时组通常是多余的):

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

dispatch_async(queue, ^{
    for (int i = 0; i < 100; i++)
    {
        dispatch_async(dispatch_get_main_queue(),^{
            self.progressBar.progress = ((CGFloat) i) / 100.0;
        });

        // do big somethings
    }
});

如果您仍然没有看到进度视图更新,那么它一定很简单IBOutlet,例如没有正确连接。确定self.progressBar不是nil。尝试将其设置progress0.5并确保正确反映。

于 2013-02-20T16:32:25.030 回答
0

我建议使用

dispatch_queue_t queue = dispatch_queue_create("com.company.myqueue", NULL);

更新进度状态,因为

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

是并发队列。

于 2014-08-19T03:02:13.930 回答
-1

您应该在主线程中更新进度条:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{

    // async stuff

    dispatch_sync(dispatch_get_main_queue(), ^{

        // sync stuff
    });
});

dispatch_release(queue);
于 2013-02-20T16:47:45.463 回答