views:

61

answers:

2

I have a bunch of identical CALayers that I want to reuse. Often, a few of them should disappear, and then get reused in another position within the same superlayer (half a second or so later).

What is the best way (performance-wise) to keep them while they have disappeared from the screen? setHidden:YES, or setOpacity:0, or removeFromSuperLayer ? Or something else I am not thinking of?

(There are about 12 identical circle shaped CALayers with contents from a UIImage, and about 30 CAShapeLayers each one holding just a line segment -though usually in different orientations-)

A: 

You should use an nsset or nsarray to maintain a queue of unused calayers. The process would be similar to what you do when using tablecells.

As each calayer is removeFromSuperLayer'd, put it into your set and pull one out from the set when you need one.

Amanuel
Thanks, but I'm doing something similar already. What I really want to know, is whether you should keep the layer as the sublayer of some layer already on screen (and just hide it or make it completely transparent) or remove it completely for the graphics hardware to be able to 'cache' it better.
baris
Hiding the layer or making it transparent will probably rob you some fps. It is best to keep the layer stack light. So I'd suggest removefromsuperlayer always when not in use.
Amanuel
A: 

The three you mentioned seem like all reasonable things to try. You really should test each one and see which gives your application the best performance, the results might surprise you.

Colin Barrett
I haven't been able to get the performance I want with any of them. I will do a more 'scientific' test when I learn to use the instruments more effectively. I've tried them with the Core Animation instrument, but the funny thing is my FPS seems to be decent with all of them - but I can notice that one of my animations is skipping (quite a few) frames.
baris
Try using the Sampling instrument or using Shark.
Colin Barrett