How do I determine if a CALayer is fully covered by other CALayers?
I have a series of layers that are randomly placed on the screen. As each layer is added, it is positioned on top of all of the others.
Eventually, a l开发者_如何转开发ayer is completely covered by other layers. At this point, I'd like to remove the layer from memory.
Is there any way to know when a layer is covered (either 100% or some fraction) by other layers?
Each layer has a rotation transform applied to it, so I cannot accurately make comparisons amongst all of the layers' frames.
You could do a pixel-test to find out. Init a grayscale context the size of your screen (if possible, it only needs to be 1-bit, though I don't know if iOS actually supports that configuration). Fill the area with black. Fill the area your layer covers with white (you can take the layer's transform, set it as the CTM, and then fill the rect for your layer). Then iterate over all other layers and do the same thing, except filling with black again. Once that's done, you can scan all the pixels in the context, looking to see if any of them are white. If you find a white pixel, the layer is still visible. Otherwise, it's not.
Naturally, this assumes that all your layers are completely opaque and fill their entire bounds.
精彩评论