确定UIView是否被其他视图覆盖? [英] Determine whether UIView is covered by other views?

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

问题描述

我正在构建一个简单的纸牌游戏,其中卡片被轻弹到中央堆上,当数量太大时我想开始移除底部卡片。但是,我对每张卡应用了一些随机旋转和定位,这意味着其中一些卡的角落伸出,等等。这是一个截图:

I'm building a simple card game where cards are flicked onto a central pile, and I'd like to begin removing the bottom cards when the number gets too large. However, I apply some random rotation and positioning to each card, meaning the corners of some of them stick out, etc. Here's a screenshot:

我想做的是确定是否有用户可以看到视图的一部分,并删除任何完全由其他视图覆盖的视图。

What I'd like to do is ascertain whether any portion of a view is visible to the user, and remove any that are completely covered by other views.

视图都是实例化的,它们都在同一个超视图中,它们都在视图的范围内,因此使用这些方法是行不通的。我需要找出一个视图是否被其他视图完全覆盖,此时我可以安全地删除它而不会让用户看到消失的东西。

The views are all instantiated, they are all inside the same superview, and they are all within the bounds of the view, so using these methods will not work. I need to find out if a view has been completely covered by other views, at which point I can safely remove it without the user seeing something vanish.

推荐答案

从概念上讲,您希望迭代堆栈,将卡的路径合并为累积路径,然后检查该联合路径是否与 next 联合路径相等。换句话说,如果向下添加下一个路径不会更改联合路径,那么必须才能完全遮挡,因此可以将其删除。它可能看起来像这样:

Conceptually, you want to iterate down the stack, unioning the paths of the cards into an accumulated path, and then checking that union path for equality with the next union path. In other words, if adding the next path down doesn't change the union path, then it must be completely obscured, and can therefore be removed. It might look something like this:

UIBezierPath* accumulator = nil;
for (UIView* cardView in [[[containingView subviews] copy] reverseObjectEnumerator])
{
    UIBezierPath* p = GetPathForView(cardView);
    UIBezierPath* next = PathByUnioningPaths(p, accumulator);

    if ([next isEqual: accumulator])
    {
        // This view is completely obscured, remove it
        [cardView removeFromSuperview];
    }

    accumulator = next;
}

当然,这假定存在函数 GetPathForView PathByUnioningPaths 。根据您创建卡片视图,前者将由您自己编写。后者将需要某种贝塞尔路径布尔运算库。我找到了这个,似乎有一点牵引力: https://bitbucket.org/martinwinter/vectorbooleancg

This, of course, presumes the existence of the functions GetPathForView and PathByUnioningPaths. The former will be yours to write, based on however you create your card views. The latter will require some sort of bezier path boolean operation library. I found this one, which seems to have a fair bit of traction: https://bitbucket.org/martinwinter/vectorbooleancg

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

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