当用户滚动UIScrollView时如何重绘UIView? [英] How do I get my UIView to redraw while the user is scrolling a UIScrollView?

查看:48
本文介绍了当用户滚动UIScrollView时如何重绘UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个子视图的根UIView:

I have a root UIView with two subviews:

  1. 自定义UIView子类
  2. UIScrollView

自定义UIView子类具有某种状态,该状态由每N秒触发的NSTimer更新.更新视图状态后,视图将调用[self setNeedsDisplay],此后不久将触发重绘.这一切都按预期进行.

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.

但是,每当用户滚动UIScrollView时,自定义UIView子类都不会重绘.用户触摸UIScrollView时,重绘就会停止.然后,当用户停止触摸UIScrollView并且滚动完全停止时,自定义UIView子类最终将重新绘制.

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.

有没有一种方法可以让用户在滚动UIScrollView时重绘自定义UIView子类?

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

推荐答案

我在Mac方面更具优势,但我认为,由于-setNeedsDisplay: future 重绘的标志,因此永远不会发生,因为滚动/触摸事件仍在占用当前(主)循环.在这种情况下,您的计时器永远不会有机会在主运行循环上触发,因为滚动尚未完成.

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.

更正

通过快速搜索可以发现,使用NSRunLoopCommonModes将您的计时器添加到运行循环中可以防止它们被暂停.这可能会为您解决问题,但您可能仍然会遇到标记以供将来显示"与立即绘制"的冲突.

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".

另一个补充

啊.似乎对此已在SO上得到解答.

这篇关于当用户滚动UIScrollView时如何重绘UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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