当用户滚动 UIScrollView 时,如何让 UIView 重绘?

发布于 2024-09-19 23:34:27 字数 380 浏览 1 评论 0原文

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

  1. 自定义 UIView 子类
  2. UIScrollView

自定义 UIView 子类有一些由每 N 秒触发的 NSTimer 更新的状态。当视图的状态更新时,视图调用 [self setNeedsDisplay],此后不久就会触发重绘。这一切都按预期进行。

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

有没有办法让自定义 UIView 子类在用户滚动 UIScrollView 时重绘?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城追梦 2024-09-26 23:34:27

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

更正

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

另一个补充

啊。似乎这个问题已经得到了回答

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文