Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView and CATiledLayer from the ground up

my situation: I created an UIScrollView with an UIView inside which calls a Graph class which draws a nice graph on a context. Now I discovered that the phone wouldn't render the UIView if it's width is larger than 8192 pixels. In fact, according to Apple's docs, if I want it larger than 1024px, I should implement CATiledLayer.

But after reading and googling a lot, I still find it difficult to understand the basics of CATiledLayer for this task-I'm somehow lost between Quartz vs. Cocoa and layers and sublayers vs. views and subviews.

Ideally I would like to keep the Graph class untouched, just draw the context entirely, split it into tiles and scroll those. The scrollview should just scroll horizontal, no zooming or vertical scrolling required. Is that possible? If so, how should I continue? Perhaps someone could give me an outline, just some bulletpoints or pseudocode, how I should restructure the scrollview, uiview and the graph class to use tiling.

Thank you very much in advance for any reply.

like image 922
marimba Avatar asked Oct 14 '22 20:10

marimba


1 Answers

It's been a month since you asked but this may still be of use. I just started using CATiledLayer tonight. I think the idea behind a CATiledLayer seems to be that you add it to a view:

CATiledLayer *tiled = [CATiledLayer layer];
[self.view.layer addSublayer:tiled];

and that you set a delegate, implementing just one method:

MyTLDelegate *myDelegate = [[MyTLDelegate alloc] init];
tiled.delegate = myDelegate;
// I haven't checked if CATiledLayer retains myDelegate, check this!

...
@implementation MyTLDelegate {

-(void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)ctx
{
    CGRect dirtyRect = CGContextGetClipBoundingBox(ctx);
    // draw!
}

That's basically all there is to it, you can just draw as if the entire coordinate space is just there. So you should only need to slightly modify your Graph class so it can act as a CATiledLayer delegate. In my case it was 20 minutes well spent, boosting user experience by several orders of magnitude. (compared to doing the tedious scrolling, scaling and redrawing myself)

ps. this is only the pseudo code you asked for, you may need some extra glue to get things going, e.g. for quality and/or frame sizes.

like image 183
mvds Avatar answered Oct 23 '22 19:10

mvds