When should I use multiple layers?
Unfortunately, the answer is: whenever necessary. Layers are cheap, and certainly lighter than UIView/UIImageView, but they are also not a part of the traditional iOS responder chain (in that they cannot directly receive/relay touches), but the cell itself usually handles that for you. Apple recommends that you avoid -drawRect:
in favor of a divided view hierarchy for cleanliness' sake, but that doesn't mean you have to have a different view for the background.
Is it possible to use the output from UIImageView in drawRect?
If you are having a problem with FPS, it's a good idea to isolate where it's coming from before optimizing away things that might not be problem spots. I would investigate the image first: are you performing any kind of caching, or does the tableview cell have to redraw it every single time it's -drawRect:
is called? Does UIImageView have to perform any kind of resizing/scaling/decompression on the image? If so, have you considered resizing the image yourself once, then saving the result into a bitmap context which you simply pull from an array and draw, rather than have UIImageView waste it's time doing it for you?
As long as your other question is closed, I'll address animation as well
Animating the table cell's background is trivial, especially if your cell is drawing it's background in Core Graphics, rather than using a CALayer (which means you'll be using a CABasicAnimation to animate to and from a background color as detailed here). I especially like TwUI's animation mechanism for selected state changes, because it's just dead simple:
- Start an animation context.
- Have a method that calls
-setNeedsDisplay
, or does some extra work you want animated.
- Commit the animations.