您的位置:首页 > 其它

Quartz 2D 绘图d

2014-10-23 11:05 134 查看

Quatrz 2D基础

Quartz 2D是iPhone OS和Mac OS X环境下的二维绘图引擎。使用Quartz 2D API,你可以接触到这样一些特性:基于路径的绘图,透明度绘图,遮盖,阴影,透明层,颜色管理,放锯齿渲染,生成PDF以及PDF元数据相关处理。Quartz 2D还可以借助硬件的力量进行图像处理。

数据类型&API

typedef struct CGContext *CGContextRef;
CGContextRef一种迷糊数据类型用于描述Quartz 2D绘制图形的绘制环境

CGContextSetLineWidth(context,10);
在绘制环境里设置绘图时线的宽度,单位是:像素

CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);
在绘图环境里设置绘图是画笔的颜色

CGContextMoveToPoint(context,30,30);
在绘制环境里,移动画笔到初始位置

CGContextAddLineToPoint(context,200,200);
在绘图环境里,绘制直线的终点

CGContextAddRect(context,rect);
在绘图环境里绘制矩形

CGContextStrokePath(context);
开始绘制图形

划线


矩形

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,10);
CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);

CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);

CGRect rt = CGRectMake(50,60,220,60);
CGContextAddRect(context,rt);
CGContextDrawPath(context,kCGPathEOFill);
}

矩形中间涂色

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context,rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor],CGColor);
CGContextFillRect(context,rectangle);
}

椭圆

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,10);
CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);

CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);

CGRect rt = CGRectMake(50,60,220,60);
CGContextAddEllipseInRect(context, rt);
CGContextDrawPath(context,kCGPathEOFill);
}

图像

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,10);
CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);

CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);

CGRect rt = CGRectMake(50,60,220,300);
[_drawImage drawInRect:rt];
}

菱形

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,2);
CGContextSetStrokeColorWithColor(context,[UIColor blueColor].CGColor);

CGContextMoveToPoint(context,100,100);
CGContextAddLineToPoint(context,150,150);
CGContextAddLineToPoint(context,100,200);
CGContextAddLineToPoint(context,50,150);
CGContextAddLineToPoint(context,100,100);

CGContextStrokePath(context);
}

菱形中间填充颜色

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(Context,100,100);
CGContextAddLineToPoint(context,150,150);
CGContextAddLineToPoint(context,100,200);
CGContextAddLineToPoint(context,50,150);
CGContextAddLineToPoint(context,100,100);

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
}

矩形边框+内部涂色

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, rectangle);
}

曲线

- (void)drawRect:(CGrect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100,100);
CGContextAddArcToPoint(context,100,200, 300,200, 100);
CGContextStrokePath(context);
}

-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 10,10);
CGContextAddCurveToPoint(context,0,50,300,250,300,400);
CGContextStrokePath(context);
}

- (void)drawRect:(CGrect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context,10,200);
CGCOntextAddQuadCurveToPoint(context,150,10,300,200);
CGContextStrokePath(context);
}

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGFloat dashArray[] = {2,6,4,2};
CGContextSetLineDash(context,3,dashArray,4);
CGContextMoveToPoint(context,10,200);
CGContextAddQuadCurveToPoint(context,150,10,300,200);
CGContextStrokePath(context);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: