5

我已经开始在 iOS5 中使用 UIProgressView,但还没有真正的运气。我在更新视图时遇到问题。每次更新进度后,我都有一组顺序操作。问题是,进度视图不会一点一点地更新,而是在全部完成之后才更新。它是这样的:

float cnt = 0.2;
for (Photo *photo in [Photo photos]) {
    [photos addObject:[photo createJSON]];
    [progressBar setProgress:cnt animated:YES];
    cnt += 0.2;
}

浏览堆栈溢出,我发现了这样的帖子 - setProgress 自 iOS 5 以来不再更新 UIProgressView,这意味着为了使其正常工作,我需要运行一个单独的线程。

我想澄清一下,我真的需要单独的线程来让 UIProgressView 正常工作吗?

4

3 回答 3

18

Yes the entire purpose of progress view is for threading.

If you're running that loop on the main thread you're blocking the UI. If you block the UI then users can interact and the UI can't update. YOu should do all heavy lifting on the background thread and update the UI on the main Thread.

Heres a little sample

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelectorInBackground:@selector(backgroundProcess) withObject:nil];

}

- (void)backgroundProcess
{    
    for (int i = 0; i < 500; i++) {
        // Do Work...

        // Update UI
        [self performSelectorOnMainThread:@selector(setLoaderProgress:) withObject:[NSNumber numberWithFloat:i/500.0] waitUntilDone:NO];
    }
}

- (void)setLoaderProgress:(NSNumber *)number
{
    [progressView setProgress:number.floatValue animated:YES];
}
于 2012-06-27T12:57:00.813 回答
2

UIProgressView.h文件中定义:

IBOutlet UIProgressView *progress1;

.m文件中:

test=1.0;
progress1.progress = 0.0;
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];

- (void)makeMyProgressBarMoving {
    NSLog(@"test    %f",test);
    float actual = [progress1 progress];
    NSLog(@"actual  %f",actual);

    if (progress1.progress >1.0){
        progress1.progress = 0.0;
        test=0.0;
    }

    NSLog(@"progress1.progress        %f",progress1.progress);
    lbl4.text=[NSString stringWithFormat:@" %i %%",(int)((progress1.progress) * 100)  ] ;
    progress1.progress = test/100.00;//actual + ((float)recievedData/(float)xpectedTotalSize);
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
    test++;

}
于 2013-04-29T12:17:36.340 回答
1

我有类似的问题。UIProgressView 没有更新,虽然我做了 setProgress 甚至尝试了 setNeedsDisplay 等。

float progress = (float)currentPlaybackTime / (float)totalPlaybackTime;
[self.progressView setProgress:progress animated:YES];

在 setProgress 部分取得进展之前,我有 (int)。如果你用 int 调用 setProgress,它不会像 UISlider 那样更新。人们应该只用从 0 到 1 的浮点值来调用它。

于 2016-10-11T18:01:11.830 回答