CGGeometry
CGGeometry.h
变换
- CGRectOffset 返回一个原点在原矩形上进行了偏移的矩形.
CGRect CGRectOffset( CGRect rect, CGFloat dx, CGFloat dy )
- CGRectInset 返回一个与原矩形共中心的,或大些或小些的新矩形
矩形将围绕它的中心点进行缩放,左右分别增减dx(总共2 x dx),上下分别增减 dy(总共 2 x dy)。CGRect CGRectInset( CGRect rect, CGFloat dx, CGFloat dy )
- CGRectIntegral 返回包围源矩形的最小整数矩形。
CGRectIntegral 将表示原点的值向下取整,表示大小的值向上取整,这样就保证了你的绘制代码平整地对齐到像素边界。CGRect CGRectIntegral( CGRect rect )
取值辅助函数
-
CGRectGet[Min|Mid|Max][X|Y]
- CGRectGetMinX
- CGRectGetMinY
- CGRectGetMidX
- CGRectGetMidY
- CGRectGetMaxX
- CGRectGetMaxY
返回矩形x或y的最小、中间或最大值
用这些函数代替诸如frame.origin.x + frame.size.width之类的代码将更加清晰、语义上更为生动的(特别是用取中间和取最大函数)。
-
CGRectGet[Width|Height] 返回矩形的高度
- CGRectGetWidth: 返回矩形的宽度
常量
CGRectZero, CGRectNull,和 CGRectInfinite
- const CGRect CGRectZero: 一个原点在(0, 0),且长宽均为 0 的常数矩形。这个零矩形与 CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) 是等价的。
- const CGRect CGRectNull: 空矩形。这个会在,比如说,求两个不相交的矩形的相交部分时返回。注意,空矩形不是零矩形。
- const CGRect CGRectInfinite: 无穷大矩形。
CGRectZero 可能是所有这些特殊矩形中最有用的了。当初始化一个视图时,它们的边框通常设置为CGRectZero,把布局放到 -layoutSubviews中。
CGRectDivide
void CGRectDivide(
CGRect rect,
CGRect *slice,
CGRect *remainder,
CGFloat amount,
CGRectEdge edge
)
CGRectDivide 用以下方式将矩形分割为两部分:
- 传入一个矩形并选择一条edge(上,下,左,右)
- 平行那个边在矩形里量出amount的长度
- 从edge 到量出的amount区域都保存到slice 参数中
- 剩余的部分保存到remainder 参数中
其中 edge 参数是一个CGRectEdge 枚举类型:
enum CGRectEdge {
CGRectMinXEdge,
CGRectMinYEdge,
CGRectMaxXEdge,
CGRectMaxYEdge
}
CGRectDivide is perfect for dividing up available space among several views (call it on subsequent remainder amounts to accommodate more than two views). Give it a try next time you're manually laying-out a UITableViewCell.
1,NSDivideRect(NSRect inRect, NSRect *slice, NSRect *rem, CGFloat amount, NSRectEdge edge); 的意思是把按amount把inRect分成两块。直接看代码结果
NSRect aRect = [sender frame];
NSRect popUpRect, textRect;
NSDivideRect(aRect, &popUpRect, &textRect, 30, NSMinXEdge);
NSString *popUpRectString = NSStringFromRect(popUpRect);
NSString *textRectString = NSStringFromRect(textRect);
NSString *aRectString = NSStringFromRect(aRect);
NSLog(@"popUpRectString -> %@", popUpRectString);popUpRectString -> {{207, 25}, {30, 32}}
NSLog(@"textRectString -> %@", textRectString);textRectString -> {{237, 25}, {52, 32}}
NSLog(@"aRectString -> %@", aRectString); aRectString -> {{207, 25}, {82, 32}}
aRect = [sender frame];
NSRect bRect = NSInsetRect(aRect, 10, 10);
NSString *bRectString = NSStringFromRect(bRect);
NSLog(@"bRectString -> %@", bRectString);bRectString -> {{217, 35}, {62, 12}}