views:

107

answers:

1

I have a root UIView with two subviews:

  1. a custom UIView subclass
  2. a UIScrollView

The custom UIView subclass has some state that is updated by an NSTimer that fires every N seconds. When the view's state is updated, the view calls [self setNeedsDisplay], which triggers a redraw shortly thereafter. This all works as expected.

However, whenever the user scrolls the UIScrollView, the custom UIView subclass does not redraw. The moment the user touches the UIScrollView, the redraws stop happening. Then, when the user stops touching the UIScrollView and the scrolling comes to a complete halt, the custom UIView subclass finally redraws.

Is there a way to get the custom UIView subclass to redraw while the user is scrolling the UIScrollView?

+1  A: 

I'm more on the Mac side but I think, since -setNeedsDisplay: flags for future redrawing, it's never happening because the scroll/touch event is still tying up the current (main) loop. In this case, your timer never gets a chance to fire on the main runloop because the scrolling isn't finished yet.

Correction

A quick search reveals adding your timers to the run loop with NSRunLoopCommonModes prevents them being paused. That may fix things for you but you may still run afoul of "flagging for future display" versus "draw it now".

Another Addition

Ah. Seems this has already been answered on SO.

Joshua Nozzi
Thanks! I didn't think the NSTimer was the culprit, so I didn't even think to search for UIScrollView/NSTimer.
Hilton Campbell
It's not to suggest that timers, run loops, and UI (oh my!) don't give me headaches, but it's definitely good to have at least a tenuous grasp on how they interact. It at least gives you good starting points for finding out just how badly you've misbehaved. :-)
Joshua Nozzi
Yeah, I just finished reading through the Run Loops documentation (http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html). Very informative.
Hilton Campbell