UIProgressView not updating?

為{幸葍}努か 提交于 2019-12-04 07:09:56

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

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

}

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.

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