CGPathRef(CGMutablePathRef)相关接口的
2018-09-11 本文已影响0人
拳战攻城师
本文主要介绍通过使用CGPathRef/CGMutablePathRef哪些相关的接口可以绘制一个图形。
使用CGPathRef画矩形
double lineWidth = 5;
//创建矩形区域及矩形区域的路径,
CGRect rect = CGRectMake(lineWidth, lineWidth, 100, 200);
CGPathRef path = CGPathCreateWithRoundedRect(rect, 1, 2, nil);
//获取图形上下文并将路径加入上下文
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];
CGContextAddPath(cr, path);
//设置线条色、填充色、线条宽度
CGContextSetRGBStrokeColor(cr, 0, 0, 255, 1);
CGContextSetRGBFillColor(cr, 255, 0, 0, 1);
CGContextSetLineWidth(cr, lineWidth);
//几个绘制函数
//CGContextFillPath(cr); //只填充
//CGContextDrawPath(cr, kCGPathFill); //同上
//CGContextStrokePath(cr); //只画线条
//CGContextDrawPath(cr, kCGPathStroke); //同上
//绘制线条并对矩形进行填充
CGContextDrawPath(cr, kCGPathFillStroke);
//清理
CGPathRelease(path);
使用CGMutablePathRef画矩形
double lineWidth = 5;
//创建矩形区域、路径,将矩形区域加入至路径中
CGRect rect = CGRectMake(lineWidth, lineWidth, 100, 200);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, rect);
//获取图形上下文,并将path加入上下文中
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];
CGContextAddPath(cr, path);
//设置线条色、填充色、线条宽度
CGContextSetRGBStrokeColor(cr, 0, 0, 255, 1);
CGContextSetRGBFillColor(cr, 255, 0, 0, 1);
CGContextSetLineWidth(cr, lineWidth);
//绘制
CGContextDrawPath(cr, kCGPathFillStroke);
//清理
CGPathRelease(path);
效果如下:
image.png平移、缩放
//获取初始状态
CGAffineTransform xf = CGAffineTransformIdentity;
//x轴平移10,y轴平移20
xf = CGAffineTransformTranslate( xf, 10,20);
//x轴缩放10倍,y轴缩放20倍。
xf = CGAffineTransformScale( xf, 10, 20);
由点连线绘制直角三角形(愚蠢的方法)
//创建三角形
int count = 100;
double lineWidth = 3;
//斜边
NSPoint *points1 = (NSPoint*)malloc(sizeof(NSPoint)*count);
for(int i=0; i<count; ++i){
points1[i] = NSMakePoint(lineWidth+i, 100-i);
}
//高
NSPoint *points2 = (NSPoint*)malloc(sizeof(NSPoint)*count);
for(int i=0; i<count; ++i){
points2[i] = NSMakePoint(lineWidth, 100-i);
}
//底边
NSPoint *points3 = (NSPoint*)malloc(sizeof(NSPoint)*count);
for(int i=0; i<count; ++i){
points3[i] = NSMakePoint(100-i, lineWidth);
}
//创建矩形区域、路径,将矩形区域加入至路径中
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddLines(path,nil, points1, count);
CGPathAddLines(path,nil, points2, count);
CGPathAddLines(path,nil, points3, count);
//获取图形上下文,并将path加入上下文中
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];
CGContextAddPath(cr, path);
//设置线条色、填充色、线条宽度
CGContextSetRGBStrokeColor(cr, 0, 0, 255, 1);
CGContextSetRGBFillColor(cr, 255, 0, 0, 1);
CGContextSetLineWidth(cr, lineWidth);
//绘制
CGContextDrawPath(cr, kCGPathFillStroke);
//清理
CGPathRelease(path);
free(points1);
free(points2);
free(points3);
通过坐标绘制三角形
int count = 100;
double lineWidth = 3;
//创建矩形区域、路径,将矩形区域加入至路径中
CGMutablePathRef path = CGPathCreateMutable();
//获取图形上下文,并将path加入上下文中
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];
CGContextAddPath(cr, path);
//高
CGContextMoveToPoint(cr,lineWidth,0);
CGContextAddLineToPoint(cr,lineWidth,count);
//斜边
CGContextMoveToPoint(cr,lineWidth,count);
CGContextAddLineToPoint(cr,count,0);
//底边
CGContextMoveToPoint(cr,count,0);
CGContextAddLineToPoint(cr,lineWidth,0);
//设置线条色、填充色、线条宽度
CGContextSetRGBStrokeColor(cr, 0, 0, 255, 1);
CGContextSetRGBFillColor(cr, 255, 0, 0, 1);
CGContextSetLineWidth(cr, lineWidth);
//绘制
CGContextDrawPath(cr, kCGPathFillStroke);
//清理
CGPathRelease(path);
效果如下:
image.png