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
Maybe
CGRectContainsRect(CGRect rect1, CGRect rect2)
to check if cgrect is inside another, this method return a bool.
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;
}
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
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;
}