Help us understand the problem. What is going on with this article?

# CGGeometryクラスの位置やサイズ操作系の関数紹介

More than 5 years have passed since last update.

# 関数

## CGSizeEqualToSize

bool CGSizeEqualToSize (CGSize size1, CGSize size2);

サンプル
```    CGSize size1 = CGSizeMake(100, 100);
CGSize size2 = CGSizeMake(100, 100);
NSLog(@"result:%@", CGSizeEqualToSize(size1, size2) ? @"同じ" : @"違う");
```

```    result:同じ
```

## CGRectContainsPoint

bool CGRectContainsPoint (CGRect rect, CGPoint point);

サンプル
```    CGRect rect = CGRectMake(30, 50, 100, 100);
CGPoint point = CGPointMake(80, 140);
NSLog(@"result:%@", CGRectContainsPoint(rect, point)? @"領域内" : @"領域外");
```

```    result:領域内
```

## CGRectContainsRect

bool CGRectContainsRect (CGRect rect1, CGRect rect2);

サンプル
```    BOOL result1 = CGRectContainsRect(CGRectMake(30, 30, 100, 100), CGRectMake(40, 40, 30, 30));
NSLog(@"result1:%@", result1 ? @"含まれる" : @"含まれない");
BOOL result2 = CGRectContainsRect(CGRectMake(30, 30, 100, 100), CGRectMake(60, 60, 100, 100));
NSLog(@"result2:%@", result2 ? @"含まれる" : @"含まれない");
```

```    result1:含まれる
result2:含まれない
```

## CGRectDivide

void CGRectDivide (
CGRect rect,
CGRect *slice,
CGRect *remainder,
CGFloat amount,
CGRectEdge edge
);

CGRectEdgeはCGRectMinXEdge,CGRectMaxXEdge,CGRectMinYEdge,CGRectMaxYEdgeの4種類。

サンプル(CGRectMinXEdge)
```    CGFloat amount = 50.0f;
CGRect rect = self.view.frame;
CGRect sliceRect = CGRectNull;
CGRect remainderRect = CGRectNull;
CGRectDivide(rect, &sliceRect, &remainderRect, amount, CGRectMinXEdge);
NSLog(@"rect:%@", NSStringFromCGRect(rect)); //rect:{{0, 0}, {320, 568}}
NSLog(@"sliceRect:%@", NSStringFromCGRect(sliceRect)); //sliceRect:{{0, 0}, {50, 568}}
NSLog(@"remainderRect:%@", NSStringFromCGRect(remainderRect)); //remainderRect:{{50, 0}, {270, 568}}
UIView *sliceView = [[UIView alloc] initWithFrame:sliceRect];
sliceView.backgroundColor = [UIColor redColor];
UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
remainderView.backgroundColor = [UIColor blueColor];
```

サンプル(CGRectMaxXEdge)
```    CGFloat amount = 50.0f;
CGRect rect = self.view.frame;
CGRect sliceRect = CGRectNull;
CGRect remainderRect = CGRectNull;
CGRectDivide(rect, &sliceRect, &remainderRect, amount, CGRectMaxXEdge);
NSLog(@"rect:%@", NSStringFromCGRect(rect)); //rect:{{0, 0}, {320, 568}}
NSLog(@"sliceRect:%@", NSStringFromCGRect(sliceRect)); //sliceRect:{{270, 0}, {50, 568}}
NSLog(@"remainderRect:%@", NSStringFromCGRect(remainderRect)); //remainderRect:{{0, 0}, {270, 568}}
UIView *sliceView = [[UIView alloc] initWithFrame:sliceRect];
sliceView.backgroundColor = [UIColor redColor];
UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
remainderView.backgroundColor = [UIColor blueColor];
```

サンプル(CGRectMinYEdge)
```    CGFloat amount = 50.0f;
CGRect rect = self.view.frame;
CGRect sliceRect = CGRectNull;
CGRect remainderRect = CGRectNull;
CGRectDivide(rect, &sliceRect, &remainderRect, amount, CGRectMinYEdge);
NSLog(@"rect:%@", NSStringFromCGRect(rect)); //rect:{{0, 0}, {320, 568}}
NSLog(@"sliceRect:%@", NSStringFromCGRect(sliceRect)); //sliceRect:{{0, 518}, {320, 50}}
NSLog(@"remainderRect:%@", NSStringFromCGRect(remainderRect)); //remainderRect:{{0, 0}, {320, 518}}
UIView *sliceView = [[UIView alloc] initWithFrame:sliceRect];
sliceView.backgroundColor = [UIColor redColor];
UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
remainderView.backgroundColor = [UIColor blueColor];
```

サンプル(CGRectMaxYEdge)
```    CGFloat amount = 50.0f;
CGRect rect = self.view.frame;
CGRect sliceRect = CGRectNull;
CGRect remainderRect = CGRectNull;
CGRectDivide(rect, &sliceRect, &remainderRect, amount, CGRectMaxYEdge);
NSLog(@"rect:%@", NSStringFromCGRect(rect)); //rect:{{0, 0}, {320, 568}}
NSLog(@"sliceRect:%@", NSStringFromCGRect(sliceRect)); //sliceRect:{{0, 518}, {320, 50}}
NSLog(@"remainderRect:%@", NSStringFromCGRect(remainderRect)); //remainderRect:{{0, 0}, {320, 518}}
UIView *sliceView = [[UIView alloc] initWithFrame:sliceRect];
sliceView.backgroundColor = [UIColor redColor];
UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
remainderView.backgroundColor = [UIColor blueColor];
```

## CGRectEqualToRect

bool CGRectEqualToRect (CGRect rect1, CGRect rect2);

2領域の位置とサイズが等しいか判定する。

サンプル
```    NSLog(@"result:%@", CGRectEqualToRect(CGRectMake(0, 0, 10, 10), CGRectMake(0, 0, 10, 10)) ? @"同じ" : @"違う");
```

```    result:同じ
```

## CGRectGetWidth

CGFloat CGRectGetWidth (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"GetWidth:%f", CGRectGetWidth(rectView.frame));
```

```    GetWidth:30.000000
```

## CGRectGetHeight

CGFloat CGRectGetHeight (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"GetHeight:%f", CGRectGetHeight(rectView.frame));
```

```    GetHeight:100.000000
```

## CGRectGetMinX

CGFloat CGRectGetMinX (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"minX:%f", CGRectGetMinX(rectView.frame));
```

```    minX:10.000000
```

## CGRectGetMidX

CGFloat CGRectGetMidX (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"midX:%f", CGRectGetMidX(rectView.frame));
```

```    midX:25.000000
```

## CGRectGetMaxX

CGFloat CGRectGetMaxX (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"maxX:%f", CGRectGetMaxX(rectView.frame));
```

```    maxX:40.000000
```

## CGRectGetMinY

CGFloat CGRectGetMinY (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"minY:%f", CGRectGetMinY(rectView.frame));
```

```    minY:10.000000
```

## CGRectGetMidY

CGFloat CGRectGetMidY (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"midY:%f", CGRectGetMidY(rectView.frame));
```

```    midY:60.000000
```

## CGRectGetMaxY

CGFloat CGRectGetMaxY (CGRect rect);

サンプル
```    UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 100)];
NSLog(@"maxY:%f", CGRectGetMaxY(rectView.frame));
```

```    maxY:110.000000
```

## CGRectInset

CGRect CGRectInset (
CGRect rect,
CGFloat dx,
CGFloat dy
);

サンプル
```    self.view.backgroundColor = [UIColor yellowColor];
CGRect insetRect = CGRectInset(self.view.frame, 30, 30);
NSLog(@"insetRect:%@", NSStringFromCGRect(insetRect)); // insetRect:{{30, 30}, {260, 508}}
UIView *insetView = [[UIView alloc] initWithFrame:insetRect];
insetView.backgroundColor = [UIColor redColor];
```

## CGRectIntegral

CGRect CGRectIntegral (CGRect rect);

サンプル
```    CGRect integralRect = CGRectMake(10.9, 20.9, 100.5, 200.3);
NSLog(@"CGRectIntegral:%@", NSStringFromCGRect(CGRectIntegral(integralRect)));
CGRect integralRect2 = CGRectMake(10.2, 20.5, 100.5, 200.3);
NSLog(@"CGRectIntegral2:%@", NSStringFromCGRect(CGRectIntegral(integralRect2)));
```

```    CGRectIntegral:{{10, 20}, {102, 202}}
CGRectIntegral2:{{10, 20}, {101, 201}}
```

## CGRectIntersection

CGRect CGRectIntersection (CGRect r1, CGRect r2);

2つの領域の重なっている箇所の領域を返す。

サンプル
```    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
view1.backgroundColor = [UIColor yellowColor];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(80, 150, 200, 200)];
view2.backgroundColor = [UIColor blueColor];

CGRect intersectionRect = CGRectIntersection(view1.frame, view2.frame);
NSLog(@"intersectionRect:%@", NSStringFromCGRect(intersectionRect)); //intersectionRect:{{80, 150}, {170, 150}}

UIView *intersectionView = [[UIView alloc] initWithFrame:intersectionRect];
intersectionView.backgroundColor = [UIColor redColor];
```

## CGRectIntersectsRect

bool CGRectIntersectsRect (CGRect rect1, CGRect rect2);

2つの領域が重なり合うか判定する。

サンプル
```    CGRect rect1 = CGRectMake(50, 50, 100, 100);
CGRect rect2 = CGRectMake(100, 100, 50, 30);
NSLog(@"result1:%@", CGRectIntersectsRect(rect1, rect2) ? @"重なる" : @"重ならない");

CGRect rect3 = CGRectMake(50, 50, 100, 100);
CGRect rect4 = CGRectMake(200, 200, 50, 30);
NSLog(@"result2:%@", CGRectIntersectsRect(rect3, rect4) ? @"重なる" : @"重ならない");
```

```    result1:重なる
result2:重ならない
```

## CGRectIsEmpty

bool CGRectIsEmpty (CGRect rect);

サンプル
```    NSLog(@"result1:%@", CGRectIsEmpty(CGRectMake(10, 10, 0, 100)) ? @"Empty" : @"Rectangle");
NSLog(@"result2:%@", CGRectIsEmpty(CGRectMake(10, 10, 10, 0)) ? @"Empty" : @"Rectangle");
NSLog(@"result3:%@", CGRectIsEmpty(CGRectNull) ? @"Empty" : @"Rectangle");
NSLog(@"result4:%@", CGRectIsEmpty(CGRectMake(10, 10, 10, 10)) ? @"Empty" : @"Rectangle");
```

```    result1:Empty
result2:Empty
result3:Empty
result4:Rectangle
```

## CGRectIsInfinite

bool CGRectIsInfinite (CGRect rect);

サンプル
```    CGRect infiniteRect = CGRectInfinite;
NSLog(@"result1:%@", CGRectIsInfinite(infiniteRect) ? @"無限" : @"無限じゃない");
NSLog(@"result2:%@", CGRectIsInfinite(CGRectMake(10, 10, 10, 10)) ? @"無限" : @"無限じゃない");
NSLog(@"result3:%@", CGRectIsInfinite(CGRectMake(0, 0, 0, 0)) ? @"無限" : @"無限じゃない");
```

```    result1:無限
result2:無限じゃない
result3:無限じゃない
```

## CGRectIsNull

bool CGRectIsNull (CGRect rect);

サンプル
```    CGRect rectNull = CGRectNull;
NSLog(@"result1:%@", CGRectIsNull(rectNull) ? @"CGRectNull" : @"CGRectNullじゃない");
NSLog(@"result2:%@", CGRectIsNull(CGRectMake(0, 0, 10, 10)) ? @"CGRectNull" : @"CGRectNullじゃない");
```

```    result1:CGRectNull
result2:CGRectNullじゃない
```

## CGRectOffset

CGRect CGRectOffset (
CGRect rect,
CGFloat dx,
CGFloat dy
);

サンプル
```    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 150, 150)];
view1.backgroundColor = [UIColor redColor];
CGRect offsetRect = CGRectOffset(view1.frame, 150, 150);
NSLog(@"offsetRect:%@", NSStringFromCGRect(offsetRect)); //offsetRect:{{160, 170}, {150, 150}}
UIView *offsetRectView = [[UIView alloc] initWithFrame:offsetRect];
offsetRectView.backgroundColor = [UIColor blueColor];
```

## CGRectStandardize

CGRect CGRectStandardize (CGRect rect);

サンプル
```    CGRect standardizeRect = CGRectStandardize(CGRectMake(0, 0, -100, -50));
NSLog(@"standardizeRect:%@", NSStringFromCGRect(standardizeRect));
```

```    standardizeRect:{{-100, -50}, {100, 50}}
```

## CGRectUnion

CGRect CGRectUnion (CGRect r1, CGRect r2);

サンプル
```    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(40, 50, 180, 180)];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(70, 90, 180, 200)];
view2.backgroundColor = [UIColor yellowColor];
CGRect unionRect = CGRectUnion(view1.frame, view2.frame);
NSLog(@"unionRect:%@", NSStringFromCGRect(unionRect));
UIView *unionView = [[UIView alloc] initWithFrame:unionRect];
unionView.backgroundColor = [UIColor clearColor];
unionView.layer.borderColor = [[UIColor blackColor] CGColor];
unionView.layer.borderWidth = 1.0;
```

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away