UIProgressView not updating?

别等时光非礼了梦想. 提交于 2019-12-21 15:38:12

问题


I have started playing with UIProgressView in iOS5, but havent really had luck with it. I am having trouble updating view. I have set of sequential actions, after each i update progress. Problem is, progress view is not updated little by little but only after all have finished. It goes something like this:

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

Browsing stack overflow, i have found posts like these - setProgress is no longer updating UIProgressView since iOS 5, implying in order for this to work, i need to run a separate thread.

I would like to clarify this, do i really need separate thread for UIProgressView to work properly?


回答1:


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];
}



回答2:


Define UIProgressView in .h file:

IBOutlet UIProgressView *progress1;

In .m file:

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++;

}



回答3:


I had similar problem. UIProgressView was not updating although I did setProgress and even tried setNeedsDisplay, etc.

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

I had (int) before progress in setProgress part. If you call setProgress with int, it will not update like UISlider. One should call it with float value only from 0 to 1.



来源:https://stackoverflow.com/questions/11226509/uiprogressview-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!