自 iOS 5 起,setProgress 不再更新 UIProgressView
自从 iOS 5 发布以来,我在进度条方面遇到了一些麻烦。下面的代码在 iOS 5 之前工作正常,但在 iOS 5 中,进度栏不再显示循环中设置的新进度。
代码预计的工作方式如下:
- 创建进度条(有效)
- 在新的后台进程中: 将初始进度设置为 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:
// set an initial progress
[progressBar setProgress: 0.25];
稍后,它会在循环中更新进度以显示下载进度:
// 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 进度处。
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:
- Create the progress bar (works)
- In a new background process: Set an initial progress of 0.25 (works)
- In the same background process: Update the progress while going thru the loop (worked in iOS 4)
Here's the code for the bar init:
// 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;
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];
}
… but the progress is not shown on the screen, the progress bar is stuck at the initial 0.25 progress that was set.
Were there any changes with the iOS 5 release that could have broken it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自从iOS 5切换以来,我已经看到很多这样的问题,我不确定为什么只有iOS 5出现问题。但主要是因为我不确定为什么之前没有问题。
在代码中,您从后台线程调用
[progressBar setProgress: [counterPercentage floatValue]];
。这是 UI 调用,不应从后台线程进行。此外,您还可以调用setNeedsDisplay
,这不需要更新progressBar
,因为UIProgressView
知道如何显示自身。 iOS 5 似乎对更新 UI 的要求更加严格,但只是达到了最佳实践的程度。在我看来,这看起来是块的完美用途。使用块你的 for 循环可以这样写:
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.
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 callsetNeedsDisplay
which is not necessary to update theprogressBar
since anUIProgressView
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.To my eye this looks like a perfect use for blocks. Using blocks your for loop could be written this way:
它在 iOS 5 中确实有效,最简单的方法是:
.h 文件:
.m 文件:
在显示 0.3 的地方,您可以输入您喜欢的任何值(0 到 1 内)
It does work in iOS 5 and the easiest way to to it is here:
.h file:
.m file:
And where it says 0.3, you can put whatever value you like (within 0 to 1)