【发布时间】:2014-04-16 17:40:13
【问题描述】:
我有这个代码来等待加载任务,显示一个 activityIndicator 视图
if (isLoading) {
self.tipView = [[BBTipsView alloc] initWithMessage:@"loading..." showLoading:YES parentView:self.view autoClose:NO];
self.tipView.needsMask = YES;
[self.tipView show];
while (isLoading) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
[self.tipView close];
}
加载视图将进行动画处理,直到 isLoading 变为 false。这是我的问题: 在主线程中运行 runloop 将阻塞主线程,直到有源事件到来或计时器触发。但是为什么加载视图在主运行循环没有返回时保持动画?
-----由 bupo 编辑----
我发现当计时器触发时,runloop 不会返回。这说明 CADisplayLink 计时器触发的动画刷新 ui 是有意义的。
Note that from the perspective of NSRunloop, NSTimer objects are not "input"—they are a special type, and one of the things that means is that they do not cause the run loop to return when they fire.
【问题讨论】:
-
现代动画与 RunLoop 关系不大。它们依赖称为 Display Links 的计时器原语,可以在后台线程上运行,以确保与显示器而不是 CPU 相关联的良好刷新率。
-
你的意思是UI刷新不在主线程上运行?
-
你得到的显示回调只是主线程。计时器是在后台运行的。
标签: objective-c multithreading cocoa animation