views:

27

answers:

1

Hi, I would like to implement a drawing class with the help of Quartz. I want to be able to save parts of what is drawn on separate layers. I want these layers to be retrievable, so I can delete/hide/show layers on command.

Can I save multiple CGLayerRef as a NSMutableArray property of my class and then be able to retrieve them? If yes, can you point me to an example.

If there are any flaws in the "architecture" above, please point me to alternative solutions that could help me accomplish layered control over graphs.

Thank you.

+1  A: 

CALayers that you create, can of course be stored in NSMutableArray, and you can work with them later on, usually by animating their properties, or asking them to redraw themselves.

Usually you create a custom UIView, create and manage layers within that view. Those layers are either member variables of that view, or you store them in an array. As things are happening in your app, your view animates the layers accordingly. Usually you want to react on touch events (which you also implement in that particular view - touchesBegan/Moved...) and animate the layers.

CALayer draws itself and caches the content for as long as you call [layer setNeedsDisplay], or it's bounds (size) are changed (well, if needsDisplayOnBoundsChange is true). Practically in all my apps I did, such redrawing happens very rarely - only if data are changed, and layer needs to redraw. Animating layers, transforming their size, rotation, changing position - layer is not redrawn during any of these. Hiding, showing, changing transparency - no redraw is required.

That "drawing class" you are talking about - you actually have only two options - either you extend CALayer and overwrite drawInContext:, or you create basic CALayer, set its delegate, and there you draw in drawLayer:inContext:. I personally prefer creating delegates.

Michal
I don't understand the last part, I don't see why I would need to modify the behavior of the CALayer class, I will just try to stack my layers into an array, I think that will be enough for me.
Interfector
I think I initially misunderstood your question a bit and thought you want to somehow store what was drawn in a layer, so that you can retrieve it later. Therefore I went on about how that is not needed as layers cache their content. If all you wanted to know is whether you can store layer instance into an array - sure, it's just an object, if you want to reference it in future you have to store it somewhere, member variable or an array.
Michal