【发布时间】:2011-06-30 01:41:56
【问题描述】:
我正在尝试解决围绕 NSTimer/NSOperationQueue 执行不一致的 UI 问题。我看到无论我使用 NSTimer 还是 NSInvocationOperation 来触发下面的代码,很多时候性能是需要的,但有几次行为很慢(如下例所示,代码有时会运行超过 1 秒)。
这是我通过 NSInvocationOperation 调用的代码:
-(void) progressAsynch{
for (count = 1; count<=100; count++) {
// Reposition Label's CGRect.
[self performSelectorOnMainThread:@selector(moveLabelToNewPosition) withObject:nil waitUntilDone:YES];
//Update the progress of the progress view
[self performSelectorOnMainThread:@selector(advanceProgressViewProgress) withObject:nil waitUntilDone:YES];
//Sleep for 10 millisecs
[NSThread sleepForTimeInterval:0.01];
//if the progress view has progressed fully call main thread to to next tasks.
if(progressView.progress == 1) {
[self performSelectorOnMainThread:@selector(processResult) withObject:nil waitUntilDone:YES];
}
}
}
NSTimer(每10毫秒触发一次)调用的方法的代码和上面的很相似,只是里面没有for循环。
显然,似乎在这个处理之外有一些东西经常降低这个性能。
很想知道您是否遇到过类似的问题,或者您是否有任何可以帮助我的建议/花絮。
提前谢谢..
【问题讨论】:
-
看起来你正在做一些简单的动画。您是否有任何理由不使用内置的 UIViews。动画功能?此外,正如文档所述,NSTimer 不能保证准确性,并且取决于处理器负载。你不能指望它有任何真正的准确性。
-
感谢您的评论。不使用内置动画功能,因为我也在为循环中的每次迭代更新标签文本。
标签: iphone nstimer nsoperationqueue