Question

I have an image view that is a subview of a larger image view. The subview can be moved around the larger image view but I'd like to be able to detect the edges of the large view so that the subview can't be moved outside of it but I'm not sure how to accomplish it.

Was it helpful?

Solution

While updating the subview's frame (it needn't be a subview, by the way, but the coordinate logic below assumes so), check that the constraint

CGRect intersection = CGRectIntersection(subview.frame, superview.bounds);
BOOL fullyContained = CGSizeEqualToSize(intersection.size, subview.size);

More compactly, as @ZevEisenberg helpfully suggests:

BOOL fullyContained = CGRectContainsRect(subview.frame, superview.bounds);

If both views belong to the same superview, which is fine so long as the visually contained view is stacked on top (appears after the larger image view in the superview's subview array), then even easier:

BOOL fullyContained = CGRectContainsRect(subview.frame, superview.frame);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top