如何确定其他CALayer是否完全覆盖了CALayer? [英] How do I determine if a CALayer is fully covered by other CALayers?

查看:58
本文介绍了如何确定其他CALayer是否完全覆盖了CALayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列随机放置在屏幕上的图层。添加每个图层时,它位于所有其他图层的顶部。

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 layer is completely covered by other layers. At this point, I'd like to remove the layer from memory.

有什么方法可以知道何时覆盖层(100%或部分)其他层?

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.

推荐答案

您可以进行像素测试以找出答案。初始化一个与屏幕大小一样的灰度上下文(如果可能,它只需为1位,尽管我不知道iOS是否实际上支持该配置)。用黑色填充区域。用白色填充您的图层覆盖的区域(您可以进行图层的变换,将其设置为CTM,然后填充图层的矩形)。然后遍历所有其他层并执行相同的操作,除了再次填充黑色。完成后,您可以扫描上下文中的所有像素,看是否有白色。如果找到白色像素,则该层仍然可见。否则,不是。

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.

这篇关于如何确定其他CALayer是否完全覆盖了CALayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆