Determine whether UIView is covered by other views?
Asked Answered
C

1

2

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:

enter image description here

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.

Catholicize answered 1/2, 2015 at 17:4 Comment(0)
P
2

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

Pegeen answered 1/2, 2015 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.