Resolved UI holding up OpenGL view

Discussion in 'Mac Programming' started by Bunkum, Apr 28, 2011.

  1. Bunkum, Apr 28, 2011
    Last edited: Apr 28, 2011

    macrumors newbie

    Jun 4, 2010
    In my application when I click and hold down on a NSButton it stops the NSTimer loop that calls my drawRect. And I'm wondering why that is so, and how to prevent it.

    I tried making my own subclass of NSButtonCell and overriding trackMouse:inRect:eek:fView:untilMouseUp: and the other subsequent tracking method associated with it. But that didn't help, though I still assume there's something in there I can do to prevent this.

    I also tried running OpenGL on another thread but it still seems to get blocked when I hold down the button.
  2. macrumors 68000


    Aug 17, 2009
    Do this:

    • subclass NSButton
    • Override -mouseDown:, -mouseDragged: and -mouseUp:
    • Do not call super in any of those overrides, except perhaps to do highlighting/unhighlighting (even the, self would be fine)
    • In -mouseDown:, handle the click, then leave (do not wait for mouse-up, handle the tracking asynchronously through the other methods)

    This should hand control over to the main runloop more often so that your UI can update as required.
  3. macrumors 65816


    Dec 16, 2010
    It sounds like NSButton (or NSButtonCell) is shifting the NSRunLoop into a different mode while it tracks the mouse.

    I'm assuming you're using either scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: or scheduledTimerWithTimeInterval:invocation:repeats: to create your timer. These methods will only add the timer to the default run-loop mode.

    Instead try using timerWithTimeInterval:target:selector:userInfo:repeats: or timerWithTimeInterval:invocation:repeats:, and then manually adding the timer to the run loop for the common mode.

    [[NSRunLoop mainRunLoop] 
  4. Bunkum, Apr 28, 2011
    Last edited: Apr 28, 2011

    thread starter macrumors newbie

    Jun 4, 2010
    Thanks you two, I had just worked it out when I checked the thread just now to update it. It was as you said jiminaus I was creating my timer using scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: on my main thread. But I solved it by just creating another thread and creating the timer from there, but I'm going to checkout your solution cause it might be more appropriate.

    Just tested it and adding it to the common mode works perfectly, thanks again.

Share This Page