当用户滚动 UIScrollView 时,如何让 UIView 重绘?
我有一个带有两个子视图的根 UIView:
- 自定义 UIView 子类
- UIScrollView
自定义 UIView 子类有一些由每 N 秒触发的 NSTimer 更新的状态。当视图的状态更新时,视图调用 [self setNeedsDisplay],此后不久就会触发重绘。这一切都按预期进行。
但是,每当用户滚动 UIScrollView 时,自定义 UIView 子类都不会重绘。当用户触摸 UIScrollView 时,重绘就会停止发生。然后,当用户停止触摸 UIScrollView 并且滚动完全停止时,自定义 UIView 子类最终会重绘。
有没有办法让自定义 UIView 子类在用户滚动 UIScrollView 时重绘?
I have a root UIView with two subviews:
- a custom UIView subclass
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更倾向于 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.