How do I get my UIView to redraw while the user is

2019-07-23 00:13发布

问题:

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:

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.