开发者

UIScrollView - how to draw content on demand?

开发者 https://www.devze.com 2023-02-21 03:44 出处:网络
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).

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:

  1. 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.
  2. Same, but keep track of which ones I've already drawn
  3. Draw them from the "outside" somehow, like from the scroll view delegate - I can't figure out how to use [@"mystring" 开发者_C百科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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消