简体   繁体   English

UIProgressView没有更新?

[英]UIProgressView not updating?

I have started playing with UIProgressView in iOS5, but havent really had luck with it. 我已经开始在iOS5中使用UIProgressView了,但还没有真正的运气。 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. 浏览堆栈溢出,我发现这些帖子 - 自iOS 5以来setProgress不再更新UIProgressView ,暗示为了使其工作,我需要运行一个单独的线程。

I would like to clarify this, do i really need separate thread for UIProgressView to work properly? 我想澄清这一点,我真的需要单独的线程让UIProgressView正常工作吗?

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. 如果您在主线程上运行该循环,则会阻止UI。 If you block the UI then users can interact and the UI can't update. 如果您阻止UI,则用户可以进行交互,并且UI无法更新。 YOu should do all heavy lifting on the background thread and update the UI on the main Thread. 你应该在后台线程上做所有繁重的工作并更新主线程上的UI。

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

Define UIProgressView in .h file: .h文件中定义UIProgressView

IBOutlet UIProgressView *progress1;

In .m file: .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++;

}

I had similar problem. 我有类似的问题。 UIProgressView was not updating although I did setProgress and even tried setNeedsDisplay, etc. UIProgressView没有更新,虽然我做了setProgress甚至尝试过setNeedsDisplay等。

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

I had (int) before progress in setProgress part. 我在setProgress部分进展之前有(int)。 If you call setProgress with int, it will not update like UISlider. 如果使用int调用setProgress,它将不会像UISlider那样更新。 One should call it with float value only from 0 to 1. 应该使用浮点值从0到1调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM