我有:
似乎NSMenu默认视图在执行动画时会阻塞主线程。我已经通过辅助线程输出time_since_last_loop与视图的drawRect:(主线程)进行了测试,只有drawRect显示了断断续续。自定义视图的drawRect从~30 fps下降到5帧几帧。
任何方式让NSMenu动画都是非阻塞的,或者与自定义视图的drawRect同时发生?
答案 0 :(得分:0)
我已经使用NSTimer和NSEventTrackingRunLoopMode来解决类似的问题。
在主线程中,创建一个计时器:
updateTimer = [[NSTimer scheduledTimerWithTimeInterval:kSecondsPerFrame target:self selector:@selector(update:) userInfo:nil repeats:YES] retain];
// kpk important: this allows UI to draw while dragging the mouse.
// Adding NSModalPanelRunLoopMode is too risky.
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSEventTrackingRunLoopMode];
然后在你的update:例程中,检查NSEventTrackingRunLoopMode:
// only allow status updates and drawing (NO show data changes) if App is in
// a Modal loop, such as NSEventTrackingRunLoopMode
NSString *runLoopMode = [[NSRunLoop currentRunLoop] currentMode];
if ( runLoopMode == NSEventTrackingRunLoopMode )
{
... do periodic update tasks that are safe to do while
... in this mode
... I *think* NSMenu is just a window
for ( NSWindow *win in [NSApp windows] )
{
[win displayIfNeeded];
}
return;
}
// do all other updates
...