iOS CGRect is inside another CGRect
Asked Answered
T

5

9

Anyone has a good solution for the following task?

I need to check if a CGRect is inside another CGRect and return a CGPoint, that gives the offset if the rect window is outside in any dimension from the containing.

thanks in advance

Therrien answered 8/6, 2015 at 15:48 Comment(3)
have you tried to write such function for your own, maybe?Brad
what offset do you refer to? the overlap? in x? in y? in both?Ob
Nope @holex, because I was expecting that this is a community where people make others jobs easier. I'll write the solution and post it for other intrests. Have a nice day :)Therrien
T
15

Swift 4.1

// Returns how much is the rect outside of the view, 0 if inside
func isRectVisibleInView(rect: CGRect, inRect: CGRect) -> CGPoint {
    var offset = CGPoint()

    if inRect.contains(rect) {
        return CGPoint(x: 0, y: 0)
    }

    if rect.origin.x < inRect.origin.x {
        // It's out to the left
        offset.x = inRect.origin.x - rect.origin.x
    } else if (rect.origin.x + rect.width) > (inRect.origin.x + inRect.width) {
        // It's out to the right
        offset.x = (rect.origin.x + rect.width) - (inRect.origin.x + inRect.width)
    }

    if rect.origin.y < inRect.origin.y {
        // It's out to the top
        offset.y = inRect.origin.y - rect.origin.y
    } else if rect.origin.y + rect.height > inRect.origin.y + inRect.height {
        // It's out to the bottom
        offset.y = (rect.origin.y + rect.height) - inRect.origin.y + inRect.height
    }


    return offset
}

Swift 3 update

// Returns how much is the rect outside of the view, 0 if inside
func isRectVisibleInView(rect: CGRect, inRect: CGRect) -> CGPoint {
    var offset = CGPoint()

    if CGRectContainsRect(inRect, rect) {
        return CGPointMake(0, 0)
    }

    if rect.origin.x < inRect.origin.x {
        // It's out to the left
        offset.x = inRect.origin.x - rect.origin.x
    } else if (rect.origin.x + rect.width) > (inRect.origin.x + inRect.width) {
        // It's out to the right
        offset.x = (rect.origin.x + rect.width) - (inRect.origin.x + inRect.width)
    }

    if rect.origin.y < inRect.origin.y {
        // It's out to the top
        offset.y = inRect.origin.y - rect.origin.y
    } else if rect.origin.y + rect.height > inRect.origin.y + inRect.height {
        // It's out to the bottom
        offset.y = (rect.origin.y + rect.height) - inRect.origin.y + inRect.height
    }


    return offset
}
Therrien answered 8/6, 2015 at 16:34 Comment(2)
I meant this functionTherrien
Thanks for the answer, however it seems have bug at the line next to comment *// It's out to the bottom . Needs to be offset.y = (self.origin.y + self.height) - (inRect.origin.y + inRect.height)Tempt
R
12

Maybe

CGRectContainsRect(CGRect rect1, CGRect rect2)

to check if cgrect is inside another, this method return a bool.

Ricebird answered 8/6, 2015 at 15:51 Comment(2)
This only answers half of the question.Urana
+1 for people like me who needed to know that this method exists :). I need the bool, not the point so thanx.Caruncle
O
3

Swift 4

Simple use let boolValue = tableView.bounds.contains(cellRect)

//CoreGraphics Module-

@available(iOS 2.0, *)
public func contains(_ point: CGPoint) -> Bool


@available(iOS 2.0, *)
public func contains(_ rect2: CGRect) -> Bool
Orris answered 31/10, 2017 at 12:49 Comment(0)
N
0

Here's a C# translation of the accepted answer for the Xamarin folk:

    CGPoint isRectVisibleInView(CGRect rect, CGRect inRect) {
        var offset = new CGPoint ();

        if (inRect.Contains(rect)) {
            return new CGPoint (0, 0);
        }

        if (rect.X < inRect.X) {
            // It's out to the left
            offset.X = inRect.X - rect.X;
        } else if (rect.GetMaxX () > inRect.GetMaxX ()) {
            // It's out to the right
            offset.X = rect.GetMaxX () - inRect.GetMaxX ();
        }

        if (rect.Y < inRect.Y) {
            // It's out to the top
            offset.Y = inRect.Y - rect.Y;
        } else if (rect.GetMaxY () > inRect.GetMaxY ()) {
            // It's out to the bottom
            offset.Y = rect.GetMaxY () - inRect.GetMaxY ();
        }


        return offset;
    }
Noman answered 29/3, 2016 at 15:56 Comment(0)
K
-1

Really? OK... here it is:

+ (CGPoint) isRect:(CGRect)inRect1 inRect:(CGRect)inRect2
{
    CGPoint offsetPoint = {0, 0};
    if (!CGRectContainsRect(inRect1, inRect2)) {
        offsetPoint.x = inRect1.origin.x - inRect2.origin.x;
        offsetPoint.y = inRect1.origin.y - inRect2.origin.y;
    }

    return offsetPoint;
}
Karmen answered 8/6, 2015 at 16:9 Comment(2)
This is only checking the origin point, doesn't calculate with the height, width. I'll post my solution soon.Therrien
He didn't ask for that: "return a CGPoint, that gives the offset if it's not inside the container rect or 0,0 if it's inside".Karmen

© 2022 - 2024 — McMap. All rights reserved.