当使用同一窗口上的另一个控件时,是否可以重绘自定义NSView? [英] Can I redraw a custom NSView while another control on the same window is being used?

查看:181
本文介绍了当使用同一窗口上的另一个控件时,是否可以重绘自定义NSView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序中,我正在执行大量的数据处理,并希望能够绘制一个自定义的NSView来直观地表示这个过程。

In a program I am working on I am performing a large amount of data crunching, and want to be able draw a custom NSView to represent the process visually.

通过使用GCD(使用dispatch_sync作为需要保存的顺序),在主线程上处理数据处理:

The data crunching is processed off the main thread by using GCD (using dispatch_sync as the order needs to be preserved):

for (int i=0; i<iterations; i++) {

    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Add a data crunching task to the queue!
    });
}

当处理开始时,我初始化一个定时器,自定义显示以定期重绘(此计时器在所有处理任务完成后失效):

When the processing is started I initialise a timer, which is used to ask the custom display to redraw at regular intervals (this timer is invalidated once all of the processing tasks are completed):

-(void)onTick:(NSTimer *)timer {

    [_customView setNeedsDisplay:YES];
    // Update some other UI object (labels etc.)    
}

我有这个工作按预期,但是我注意到现在的唯一问题是,如果我做一些事情像点击并按住同一个窗口上的按钮,或开始拖动滑块,而进程正在进行,重新绘制自定义视图阻止,直到我放开鼠标!

I have this working as expected, however the only problem I notice now is that if I do something like click and hold on a button on the same window, or begin dragging a slider while the process is going on, the redrawing of the custom view is prevented until I let go of the mouse!

有没有办法,我可以防止这种情况发生,最好不要将所有的控制移动到一个单独的窗口! p>

Is there any way I can prevent this from happening, preferably without moving all of the controls onto a separate window!

推荐答案

yip的定时器是在runloop上,而在拖放/跟踪模式下,它不会被触发,因为runloop is not advanced共同模式

yip the timer is on the runloop and while in the drag drop / tracking mode it doesnt get fired because the runloop isnt advanced for the common mode

将其添加到所需的模式:

add it to the modes needed:

NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(myTimerAction:) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode: NSEventTrackingRunLoopMode];



编辑:runloop基本上是apple的while循环,用于向应用程序分派ANY事件。并根据所谓的模式过滤其分派的事件。

edit: the runloop is basically apple's while loop for dispatch ANY event to an app. and it filters the events it dispatches based on so called modes.

这篇关于当使用同一窗口上的另一个控件时,是否可以重绘自定义NSView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆