How do I get my UIView to redraw while the user is scrolling a UIScrollView?

自作多情 提交于 2019-12-24 17:29:56

问题


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.



来源:https://stackoverflow.com/questions/3729401/how-do-i-get-my-uiview-to-redraw-while-the-user-is-scrolling-a-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!