从 iOS 5 开始,setProgress 不再更新 UIProgressView [英] setProgress is no longer updating UIProgressView since iOS 5
问题描述
自从 iOS 5 发布以来,我的进度条有点问题.下面的代码在 iOS 5 之前运行良好,但在 iOS 5 中,进度条不再显示在循环中设置的新进度.
I have a little trouble with a progress bar since iOS 5 came out. The code below was working fine before iOS 5 but with iOS 5 the progress bar is no longer displaying the new progress that is set within a loop.
代码应该是这样工作的:
The code is expected to work like this:
- 创建进度条(工作)
- 在新的后台进程中:将初始进度设置为 0.25(有效)
- 在同一后台进程中:通过循环更新进度(在 iOS 4 中工作)
这是栏初始化的代码:
// create a progress bar
UIProgressView *progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(coverSizeX*0.25, coverSizeY - 34.0, coverSizeX*0.5, 9.0)];
progressBar.progress = 0.0;
progressBar.progressViewStyle = UIProgressViewStyleBar;
在另一个线程中,它将进度的起点设置为 0.25:
and in a different thread it sets a starting point for the progress to 0.25:
// set an initial progress
[progressBar setProgress: 0.25];
稍后它会在循环中更新进度以显示下载进度:
a little later it is updating the progress within a loop to display the download progress:
// within a for-loop:
NSNumber *counterPercentage;
for ( pageDownload = 1; pageDownload < pagesToDownload; pageDownload++ ) {
counterPercentage = [[NSNumber alloc] initWithFloat: (float)pageDownload / (float)((float)pagesToDownload)];
[progressBar setProgress: [counterPercentage floatValue]];
[progressBar performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
}
...但是屏幕上没有显示进度,进度条卡在最初设置的 0.25 进度.
… but the progress is not shown on the screen, the progress bar is stuck at the initial 0.25 progress that was set.
iOS 5 版本是否有任何可能破坏它的更改?
Were there any changes with the iOS 5 release that could have broken it?
推荐答案
自从 iOS 5 切换以来,我已经看到很多这样的问题,我不知道为什么只有在 iOS 5 中才会出现问题.但主要是因为我不确定为什么之前没有问题.
I've seen a lot of questions like this one since the iOS 5 switch, and I'm not sure why there is a problem only in iOS 5. But mainly because I'm not sure why there wasn't a problem before.
在您的代码中,您从后台线程调用 [progressBar setProgress: [counterPercentage floatValue]];
.这是一个 UI 调用,不应从后台线程进行.您还调用了 setNeedsDisplay
,因为 UIProgressView
知道如何显示自己,所以不需要更新 progressBar
.iOS 5 似乎对更新 UI 的要求更加严格,但只是达到了最佳实践的程度.
In your code you call [progressBar setProgress: [counterPercentage floatValue]];
from a background thread. This is a UI call and should not be made from a background thread. Also you call setNeedsDisplay
which is not necessary to update the progressBar
since an UIProgressView
knows how to display itself. iOS 5 seems to have made the requirements for updating the UI more stringent, but only to the point of what are best practices anyway.
在我看来,这看起来是块的完美用途.使用块你的 for 循环可以这样写:
To my eye this looks like a perfect use for blocks. Using blocks your for loop could be written this way:
for ( pageDownload = 1; pageDownload < pagesToDownload; pageDownload++ ) {
// Other stuff in background
dispatch_async(dispatch_get_main_queue(), ^{
progressBar.progress = ((float)pageDownload/(float)pagesToDownload);
});
// Other stuff in backgroud
}
这篇关于从 iOS 5 开始,setProgress 不再更新 UIProgressView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!