玩两个CGRect

时间:2016-05-16 13:58:29

标签: objective-c cgrect

我有两个矩形R1和R2

---------------------------------
|R1                             |
|                               |
|     ------------------        |
|     |                |        |
|     |  R2            |        |
|     |                |        |
---------------------------------

R1表示最大的一个,R2表示最小的一个。我想发送我们所拥有的区域,如果我们将R2减去R1到我的方法,将rect作为输入。这甚至可能吗?任何躲避的措施

2 个答案:

答案 0 :(得分:1)

  

如果我们将R2减去R1,我希望将我们所拥有的区域发送到我的方法,该方法将rect作为输入。这甚至可能吗?

不,因为区域 size (CGSize)表示,但您的方法将 rect (CGRect)作为输入。

但也许当你说" area"你的意思是"塑造"?然后你必须自己绘制或导出形状。而且,在这种情况下,您无法将其发送到"将矩形作为输入"的方法,因为您现在拥有的将是路径(例如UIBezierPath)或者CGPath)或者绘图(例如UIImage)。

因此,正如您在此屏幕截图中看到的那样,可以轻松制作切出的矩形形状:

enter image description here

...但描述该形状的信息不能仅仅表示为矩形。

答案 1 :(得分:0)

查找区域

-(CGSize)areaOfRect:(CGRect)outerRect minusRect:(CGRect)innerRect
{
    return (outerRect.size.width - innerRect.size.width) * (outerRect.size.height - innerRect.size.height);
}

查找rects(假设这两个rects正在使用相同的边界)

typedef void(^RectCompletion)(CGRect topRect,CGRect leftRect,CGRect rightRect,CGRect bottomRect);


-(void)rectsForRect:(CGRect)outerRect minusRect:(CGRect)innerRect withCompletion:(RectCompletion)completion
{
    // First sanity..
    // You'll need to build up a list of prerequisite checks

    if (innerRect.origin.x < outerRect.origin.x)
        outerRect.origin.x = innerRect.origin.x;

    if (innerRect.origin.y < outerRect.origin.y)
        outerRect.origin.y = innerRect.origin.y;

    if (innerRect.origin.x + innerRect.size.width > outerRect.origin.x + outerRect.size.width)
        NSLog(@"Trim to edge");

    if (innerRect.origin.x > outerRect.origin.x + outerRect.size.width || innerRect.origin.y > outerRect.origin.y + outerRect.size.width)
        NSLog(@"Trim to edge");

    // etc...


    // Assuming the rect is always in bounds below should give you the 4 rects surrounding the inner rect.. 

    // With overlap..

    CGRect topRect = CGRectMake(outerRect.origin.x, outerRect.origin.y, outerRect.size.width, innerRect.origin.y);
    CGRect leftRect = CGRectMake(outerRect.origin.x, outerRect.origin.y, innerRect.origin.x, outerRect.size.height);
    CGRect rightRect = CGRectMake(innerRect.origin.x + innerRect.size.width, outerRect.origin.y, outerRect.size.width - (innerRect.origin.x + innerRect.size.width), outerRect.size.height);
    CGRect bottomRect = CGRectMake(outerRect.origin.x, outerRect.size.height - (innerRect.origin.y + innerRect.size.height), outerRect.size.width, outerRect.size.height - (innerRect.origin.y + innerRect.size.height));


    // No overlap

    CGRect topRect = CGRectMake(outerRect.origin.x, outerRect.origin.y, outerRect.size.width, innerRect.origin.y);
    CGRect leftRect = CGRectMake(outerRect.origin.x, innerRect.origin.y, innerRect.origin.x, innerRect.size.height);
    CGRect rightRect = CGRectMake(innerRect.origin.x + innerRect.size.width, innerRect.origin.y, outerRect.size.width - (innerRect.origin.x + innerRect.size.width), innerRect.size.height);
    CGRect bottomRect = CGRectMake(outerRect.origin.x, outerRect.size.height - (innerRect.origin.y + innerRect.size.height), outerRect.size.width, outerRect.size.height - (innerRect.origin.y + innerRect.size.height));

    completion(topRect,leftRect,rightRect,bottomRect);
}