LoginSignup
51

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-02-14

はじめに

位置やサイズ、領域操作系のCGGeometryって便利だなと思って分かるところだけ簡単に調べてみました。
間違いがありましたらご指摘ください。

関数

CGSizeEqualToSize

bool CGSizeEqualToSize (CGSize size1, CGSize size2);

指定した2サイズが等しいか判定する。

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

CGRectContainsPoint

bool CGRectContainsPoint (CGRect rect, CGPoint point);

第2引数で指定した位置が第1引数で指定した領域内か判定する。

サンプル
    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);

第1引数の領域内に第2引数の領域が含まれるか判定。

サンプル
    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
);

第1引数の領域を指定方向、指定サイズで分割する。

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];
    [self.view addSubview:sliceView];
    UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
    remainderView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:remainderView];

結果(CGRectMinXEdge)

サンプル(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];
    [self.view addSubview:sliceView];
    UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
    remainderView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:remainderView];

結果(CGRectMaxXEdge)

サンプル(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];
    [self.view addSubview:sliceView];
    UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
    remainderView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:remainderView];

結果(CGRectMinYEdge)

サンプル(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];
    [self.view addSubview:sliceView];
    UIView *remainderView = [[UIView alloc] initWithFrame:remainderRect];
    remainderView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:remainderView];

結果(CGRectMaxYEdge)


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);

指定した領域の最小x座標を返す。

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

CGRectGetMidX

CGFloat CGRectGetMidX (CGRect rect);

指定した領域の中心x座標を返す。

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

CGRectGetMaxX

CGFloat CGRectGetMaxX (CGRect rect);

指定した領域の最大x座標を返す。

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

CGRectGetMinY

CGFloat CGRectGetMinY (CGRect rect);

指定した領域の最小y座標を返す。

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

CGRectGetMidY

CGFloat CGRectGetMidY (CGRect rect);

指定した領域の中心y座標を返す。

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

CGRectGetMaxY

CGFloat CGRectGetMaxY (CGRect rect);

指定した領域の最大y座標を返す。

サンプル
    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
);

指定した領域のdx,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];
    [self.view addSubview:insetView];

結果


CGRectIntegral

CGRect CGRectIntegral (CGRect rect);

指定した領域の座標xの小数点以下を幅(width)に足して繰り上げ、座標yの小数点以下を高さ(height)に足して繰り上げした領域を返す。

サンプル
    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];
    [self.view addSubview:view1];

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

    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];
    [self.view addSubview:intersectionView];

結果


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);

幅か高さが0、もしくはCGRectNullか判定する。

サンプル
    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);

指定領域がNULLか判定する。

サンプル
    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
);

指定領域の座標xにdxを加算、座標yにdyを加算した領域を返す。

サンプル
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 150, 150)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    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];
    [self.view addSubview:offsetRectView];

結果


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);

指定した2領域を覆う領域を返す。

サンプル
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(40, 50, 180, 180)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(70, 90, 180, 200)];
    view2.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view2];
    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;
    [self.view addSubview:unionView];

結果

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51