NSArrayからCGRectとか(CGGeometryとUIGeometry)

swift移植版はこっち

サンプル

こういうことができるようになります

  NSArray *a = @[@(1),@(2),@(3), @(4)];

  CGPoint point = a.CGPoint; // = (CGPoint){1,2}
  CGVector vector = a.CGVector // = (CGVector){1,2}
  CGSize size = a.CGSize; // = (CGSize){1,2}
  CGRect rect = a.CGRect; // = (CGRect){1,2,3,4}
  CGAffineTransform Transform = a.CGAffineTransform // = (CGAffineTransform){1, 2, 3, 4, 0, 0}; <- 配列数が足りない部分は0になる

  UIEdgeInsets edge =  a.UIEdgeInsets // = (UIEdgeInsets){1,2,3,4};
  UIOffset offset = a.UIOffset // = (UIOffset){1, 2};

色々座標を計算し終わったあとにCGPointMakeとか(CGPoint)とかしなくていいのでちょっとだけ楽です

ソース

ヘッダ

NSArray+Geometry.h

#import <Foundation/Foundation.h>

@interface NSArray (Geometry)
-(CGPoint)CGPoint;
-(CGVector)CGVector;
-(CGSize)CGSize;
-(CGRect)CGRect;
-(CGAffineTransform)CGAffineTransform;
-(UIEdgeInsets)UIEdgeInsets;
-(UIOffset)UIOffset;
@end

実装

NSArray+Geometry.m

#import "NSArray+Geometry.h"

@implementation NSArray (Geometry)

-(CGPoint)CGPoint{
  CGFloat x = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat y = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  return (CGPoint){x,y};
}

-(CGVector)CGVector{
  CGFloat x = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat y = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  return (CGVector){x,y};
}

-(CGSize)CGSize{
  CGFloat w = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat h = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  return (CGSize){w,h};
}

-(CGRect)CGRect{
  CGFloat x = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat y = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  CGFloat w = self[2] ? ((NSNumber*)self[2]).floatValue : 0;
  CGFloat h = self[3] ? ((NSNumber*)self[3]).floatValue : 0;
  return (CGRect){x, y , w, h};
}

-(CGAffineTransform)CGAffineTransform{
  CGFloat a = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat b = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  CGFloat c = self[2] ? ((NSNumber*)self[2]).floatValue : 0;
  CGFloat d = self[3] ? ((NSNumber*)self[3]).floatValue : 0;
  CGFloat dx = self[4] ? ((NSNumber*)self[4]).floatValue : 0;
  CGFloat dy = self[5] ? ((NSNumber*)self[5]).floatValue : 0;
  return (CGAffineTransform){a, b, c, d, dx, dy};

}

-(UIEdgeInsets)UIEdgeInsets{
  CGFloat top = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat left = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  CGFloat bottom = self[2] ? ((NSNumber*)self[2]).floatValue : 0;
  CGFloat right = self[3] ? ((NSNumber*)self[3]).floatValue : 0;
  return (UIEdgeInsets){top, left, bottom, right};
}

-(UIOffset)UIOffset{
  CGFloat horizontal = self[0] ? ((NSNumber*)self[0]).floatValue : 0;
  CGFloat vertical = self[1] ? ((NSNumber*)self[1]).floatValue : 0;
  return (UIOffset){horizontal, vertical};
}
@end

おわり

すっごいくだらない気もするがちょくちょく使うから置いときました

カテゴリ:Default 時間:2017-08-30 人気:1
この記事では、 Objective-C iOS CoreGraphics

関連記事

Copyright (C) socapnw.com, All Rights Reserved.

Socapnw All Rights Reserved.

processed in 1.074 (s). 9 q(s)