美文网首页iOS开发的正确姿势
【CoreGraphics】CGGeometry - CGPoi

【CoreGraphics】CGGeometry - CGPoi

作者: 居然是村长 | 来源:发表于2016-04-03 13:26 被阅读174次

主要还是 CGRect,其余都是辅助。

CGPoint 点(位置)

struct CGPoint {
    CGFloat x;
    CGFloat y;
};  CGPoint aPoint = {100,200};

// 一般方法
    CGPoint point = CGPointMake(100, 200);
    CGPoint zeroPoint = CGPointZero;
    BOOL isequal = CGPointEqualToPoint(point, zeroPoint);

CGSize 大小

struct CGSize {
    CGFloat width;
    CGFloat height;
};  CGSize aSize = {100,200};


//一般方法
    CGSize size = CGSizeMake(100, 200);
    CGSize zeroSize = CGSizeZero;
    BOOL isequal = CGSizeEqualToSize(size, zeroSize);
    

CGVector 矢量(偏移)

struct CGVector {
    CGFloat dx;
    CGFloat dy;
};  CGVector aVector = {20,40};

// 一般方法
    CGVector vector = CGVectorMake(20, 40);

CGRect 位置 + 大小

  • 创建
struct CGRect {
    CGPoint origin;
    CGSize size;
};  CGRect aRect = {{100,200},{120,240}};

    CGRect rect = CGRectMake(100, 200, 120, 240);
    CGRect zeroRect = CGRectZero;
    CGRect nullRect = CGRectNull;
    CGRect infiniteRect = CGRectInfinite;// 无穷大
  • 获取属性

相比 self.view.frame.size.width 获取宽方便不少!

    CGFloat minX = CGRectGetMinX(aRect);// == aRect.origin.x;
    CGFloat minY = CGRectGetMinY(aRect);// == aRect.origin.y;
    
    CGFloat maxX = CGRectGetMaxX(aRect);// == aRect.origin.x + aRect.size.width;
    CGFloat maxY = CGRectGetMaxY(aRect);// == aRect.origin.y + aRect.size.height;
    
    CGFloat midX = CGRectGetMidX(aRect);// == aRect.origin.x + aRect.size.width / 2;
    CGFloat midY = CGRectGetMidY(aRect);// == aRect.origin.y + aRect.size.height / 2;
    
    CGFloat width = CGRectGetWidth(aRect); // == aRect.size.width;
    CGFloat height = CGRectGetHeight(aRect);// == aRect.size.height;
  • 判断
    BOOL isEqual = CGRectEqualToRect(aRect, bRect);
    BOOL isEmpty = CGRectIsEmpty(aRect);
    BOOL isNull = CGRectIsNull(aRect);
    BOOL isInfinite = CGRectIsInfinite(aRect);// 无穷大
    BOOL isIntersect = CGRectIntersectsRect(aRect, bRect);// 相交
    
    BOOL iscontainPoint = CGRectContainsPoint(aRect, aPoint);
    BOOL iscontainRect = CGRectContainsRect(aRect, bRect);
    
  • 处理
    在重写控件rect 时,很好用。
    CGRectIntegral(aRect);// 转换成 整数
    CGRectUnion(aRect, bRect);// 并集
    CGRectIntersection(aRect, bRect);// 交集
    
    CGRectInset(aRect, 10, 20);// 中心不变,水平,竖直方向,两边各向内调整(负值向外)
    CGRectOffset(aRect, 10, 20);// (0,0)开始偏移,只变化了point 不变size
    CGRectStandardize(aRect);// 宽高负数时调整成标准的 :例如{{100,100},{-90,-80}} ==》 {{10,20},{90,80}}
  • 分割
typedef CF_ENUM(uint32_t, CGRectEdge) {
    CGRectMinXEdge, CGRectMinYEdge, CGRectMaxXEdge, CGRectMaxYEdge
};// 切割 起始位置

    CGRect rect =CGRectMake(100,100, 200,300);
    CGRect slice;// 切掉
    CGRect remainder;// 剩下
    CGRectDivide(rect, &slice, &remainder,40, CGRectMaxYEdge);// 最后的参数:以哪边开始算起 amount 的 数值,切掉。
    

    UIView *view1 = [[UIView alloc]initWithFrame:slice];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc]initWithFrame:remainder];
    view2.backgroundColor = [UIColor greenColor];;
    [self.view addSubview:view2];

其他

CFDictionaryRef 相关 暂无研究


/*** Persistent representations. ***/

/* Return a dictionary representation of `point'. */

CG_EXTERN CFDictionaryRef  CGPointCreateDictionaryRepresentation(
    CGPoint point)
    CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/* Make a CGPoint from the contents of `dict' (presumably returned earlier
   from `CGPointCreateDictionaryRepresentation') and store the value in
   `point'. Returns true on success; false otherwise. */

CG_EXTERN bool CGPointMakeWithDictionaryRepresentation(
    CFDictionaryRef __nullable dict, CGPoint * __nullable point)
    CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/* Return a dictionary representation of `size'. */

CG_EXTERN CFDictionaryRef  CGSizeCreateDictionaryRepresentation(CGSize size)
    CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/* Make a CGSize from the contents of `dict' (presumably returned earlier
   from `CGSizeCreateDictionaryRepresentation') and store the value in
   `size'. Returns true on success; false otherwise. */

CG_EXTERN bool CGSizeMakeWithDictionaryRepresentation(
    CFDictionaryRef __nullable dict, CGSize * __nullable size)
    CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/* Return a dictionary representation of `rect'. */

CG_EXTERN CFDictionaryRef  CGRectCreateDictionaryRepresentation(CGRect)
    CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/* Make a CGRect from the contents of `dict' (presumably returned earlier
   from `CGRectCreateDictionaryRepresentation') and store the value in
   `rect'. Returns true on success; false otherwise. */

CG_EXTERN bool CGRectMakeWithDictionaryRepresentation(
    CFDictionaryRef __nullable dict, CGRect * __nullable rect)
    CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

相关文章

网友评论

    本文标题:【CoreGraphics】CGGeometry - CGPoi

    本文链接:https://www.haomeiwen.com/subject/pzlulttx.html