我有:
似乎NSMenu默认视图在执行动画时阻塞了主线程。我对此进行了测试,方法是让辅助线程输出time_since_last_loop与视图的drawRect: of (这是主线程),并且只有drawRect显示口吃。定制视图的drawRect从30 fps下降到5 fps,用于几个帧。
有什么方法可以使NSMenu动画非阻塞,或者与自定义视图的drawRect并发?
发布于 2017-01-06 20:23:38
我使用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];然后在更新:例程中,检查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
...https://stackoverflow.com/questions/12696466
复制相似问题