Determine whether UIView is covered by other views?

让人想犯罪 __ 提交于 2019-12-12 09:19:29

问题


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.


回答1:


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;
}

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



来源:https://stackoverflow.com/questions/28265287/determine-whether-uiview-is-covered-by-other-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!