UIScrollView - 如何按需绘制内容?
我想创建一个具有大量 contentSize 的滚动视图,该视图将在其中显示内容。内容主要是文本(将绘制一些小图像作为内容边界)。
因此,请像平铺地图应用程序一样思考,但平铺标签而不是平铺图像。
性能在此应用程序中至关重要,因此我认为我应该避免在滚动视图内使用 UILabels 或任何 UIView。
当用户滚动时如何绘制这些标签?我考虑过的一些选项:
- 在滚动视图中覆盖drawRect:并在窗口内绘制所有内容 - 这看起来会非常慢。有时调用drawRect时只有1个像素的差异。
- 相同,但跟踪我已经绘制了哪些
- 以某种方式从“外部”绘制它们,例如从滚动视图委托中 - 我不知道如何在drawRect:之外使用[@“mystring”drawInRect:](上下文问题)
有什么我没有想到的吗?我知道滚动视图被设计为能够处理此类问题,但我不确定设计的方式是什么。谢谢!
I want to create a scroll view with a massive contentSize that will display content inside it. The content will be mostly text (a few small images will be drawn for content-boundaries).
So, think like a tiled map application, but tiling labels instead of tiled images.
Performance is critical in this application, so I think I should avoid using UILabels or any UIViews at all inside the scroll view.
How can I draw these labels as the user scrolls around? Some options I've considered:
- override drawRect: in the scroll view and draw everything within the window - this seems like it would be really slow. Sometimes drawRect is called with only a 1 pixel difference.
- Same, but keep track of which ones I've already drawn
- Draw them from the "outside" somehow, like from the scroll view delegate - I can't figure out how to use [@"mystring" drawInRect:] outside of drawRect: (context problems)
Is there something I haven't thought of? I know the scroll views were designed to be able to handle this kind of problem, but I'm not sure what the designed way is. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 iPhone 应用程序中实现此目的的标准方法是创建一个正常的 UIScrollView (其大小为您想要的大小),并直接使用 CATiledLayer 如果您有勇气或使用使用 CATiledLayer 并实现
的自定义 UIView 子类- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
。CATiledLayer 提供与 Safari 相同的体验 - 它适用于比屏幕大得多且渲染成本昂贵的视图,但您又不想破坏用户体验的流畅性。当需要在后台线程上提供图块时,它将请求图块,然后在它们准备好时将它们淡入(根据任何长度的淡入淡出,以便您可以根据需要使它们立即出现)。如果您的程序确实始终可以跟上滚动,并且您已请求立即出现,那么就没有证据表明涉及异步步骤。
人们倾向于指出的一个例子是这个,但如果你是新手要实现您自己的 UIView 子类,那么可能值得一看,例如 本教程。然后查看 + layerClass 属性和 CATiledLayer 的各种属性(我认为您可能还需要子类化 + fadeDuration 是一个类方法)。
The standard way to achieve this in an iPhone application is to create a normal UIScrollView that is the size you want it to be, and to populate it either directly with a CATiledLayer if you're feeling brave or with a custom UIView subclass that uses a CATiledLayer and implements
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
.CATiledLayer provides the same experience as Safari — it's good for views that are much larger than the screen and which are expensive to render, but which you don't want to ruin the fluidity of the user experience. It'll request tiles as and when it needs them on a background thread, then fade them in (according to a fade of any length, so you can cause them to appear instantly if you desire) when they're ready. If your program really can always keep up with the scrolling and you've requested an instant appearance then there'll be no evidence that there's an asynchronous step involved.
An example people tend to point to is this one, but if you're new to implementing your own UIView subclasses then it might be worth seeing e.g. this tutorial. Then look into the + layerClass property on UIView and the various properties of CATiledLayer (which I think you'll also possibly need to subclass since + fadeDuration is a class method).