使用到:
CGRectInset
CGRectoffset
UIEdgeInsetsInsetRect
这三个函数的使用情况
//CGRectInset 将原来的矩形放大或者缩小,正表示缩小,-表示放大。
CGRect rect= CGRectMake(20, 50, 100, 80);
CGRect rect1=CGRectInset(rect, -10, 20);
NSLog(@"%@",p(rect1));
//输出结果:2014-11-22 18:48:55.351 TestCGRectInset[8893:60b] {{10, 70}, {120, 40}}
//CGRectOffset 这个函数就是将原来矩形的坐标点变化一下,就是左上角点
CGRect rect= CGRectMake(20, 50, 100, 80);
CGRect rect1=CGRectOffset(rect, -10, 20);
NSLog(@"%@",p(rect1));
//输出结果:2014-11-22 18:51:58.217 TestCGRectInset[8913:60b] {{10, 70}, {100, 80}}
//UIEdgeInsetsInsetRect 表示在原来的rect基础上根据边缘距离内切一个rect出来
CGRect rect= CGRectMake(20, 50, 100, 80);
UIEdgeInsets ed=UIEdgeInsetsMake(-3, -4, -5, -6);
CGRect r= UIEdgeInsetsInsetRect(rect, ed);
NSLog(@"%@",p(r));
绘制代码如下:
- (void)drawRect:(CGRect)rect
{
// rect.size.height = 140;
// rect.size.width = 180;
// rect请设置为屏幕宽高
const CGFloat RECT_PADDING = 8.0;
rect = CGRectInset(rect, (rect.size.width-180)/2, (rect.size.height-140)/2);
const CGFloat ROUND_RECT_CORNER_RADIUS = 5.0;
CGPathRef roundRectPath = NewPathWithRoundRect(rect, ROUND_RECT_CORNER_RADIUS);
CGContextRef context = UIGraphicsGetCurrentContext();
const CGFloat BACKGROUND_OPACITY = 0.85;
CGContextSetRGBFillColor(context, 0, 0, 0, BACKGROUND_OPACITY);
CGContextAddPath(context, roundRectPath);
CGContextFillPath(context);
const CGFloat STROKE_OPACITY = 0.25;
CGContextSetRGBStrokeColor(context, 1, 1, 1, STROKE_OPACITY);
CGContextAddPath(context, roundRectPath);
CGContextStrokePath(context);
CGPathRelease(roundRectPath);
}
CGPathRef NewPathWithRoundRect(CGRect rect, CGFloat cornerRadius)
{
//
// Create the boundary path
//
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL,
rect.origin.x,
rect.origin.y + rect.size.height - cornerRadius);
// Top left corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x,
rect.origin.y,
rect.origin.x + rect.size.width,
rect.origin.y,
cornerRadius);
// Top right corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x + rect.size.width,
rect.origin.y,
rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height,
cornerRadius);
// Bottom right corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height,
rect.origin.x,
rect.origin.y + rect.size.height,
cornerRadius);
// Bottom left corner
CGPathAddArcToPoint(path, NULL,
rect.origin.x,
rect.origin.y + rect.size.height,
rect.origin.x,
rect.origin.y,
cornerRadius);
// Close the path at the rounded rect
CGPathCloseSubpath(path);
return path;
}
如图: