Quartz 2D小Demo
本次小Demo分2块,一个是自己写的小黄人的头部实战使用.确实很挫..里面主要是一些圆,弧,线,的使用.
第二个是找到的关于基础使用比较全的一个Demo.
代码1.小黄人:
- (void)drawRect:(CGRect)rect
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.身体
drawBody(ctx, rect);
// 3.嘴
drawZui(ctx, rect);
// 3.画眼睛
drawEyes(ctx, rect);
}
/**
眼睛
/
void drawEyes(CGContextRef ctx, CGRect rect)
{
// 0.其他变量
CGFloat kuangXPading = 5;
CGFloat eyesBianKuangHight = 15; // 眼睛边框的高度
// 1.黑色绑带
CGFloat startX = rect.size.width 0.5 - BMRadius;
CGFloat startY = BMTopY;
CGContextMoveToPoint(ctx, startX, startY);
CGFloat endX = startX + 2 * BMRadius;
CGFloat endY = startY;
CGContextAddLineToPoint(ctx, endX, endY);
CGContextSetLineWidth(ctx, 30); // 绑带线条
[[UIColor blackColor] set];
// 绘制线条
CGContextStrokePath(ctx);//画眼镜框边框(左)
CGFloat EyesBianKuangRadius = BMRadius 0.96;
CGFloat EyesBianKuangX = rect.size.width 0.5 - EyesBianKuangRadius + kuangXPading;
CGFloat EyesBianKuangY = BMTopY - eyesBianKuangHight ;
CGContextMoveToPoint(ctx, EyesBianKuangX, EyesBianKuangY);
CGFloat endEyesBianKuangX = EyesBianKuangX;
CGFloat endEyesBianKuangY = BMTopY + eyesBianKuangHight;
CGContextAddLineToPoint(ctx, endEyesBianKuangX, endEyesBianKuangY);
CGContextSetLineWidth(ctx, 10); // 绑带线条
CGContextSetLineCap(ctx, kCGLineCapRound);
[BMColor(142, 142, 142) set];
CGContextStrokePath(ctx);// 2.最外圈的镜框
[BMColor(92, 92, 92) set];
CGFloat kuangRadius = EyesBianKuangRadius 0.5;
CGFloat kuangY = startY;
CGFloat kuangX = rect.size.width 0.5 - kuangRadius + kuangXPading;
CGContextAddArc(ctx, kuangX, kuangY, kuangRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);// 3.里面的皮肤框
[BMColor(252, 198, 0) set];
CGFloat yellowRadius = kuangRadius * 0.76;
CGFloat yellowX = kuangX;
CGFloat yellowY = kuangY;CGContextAddArc(ctx, yellowX, yellowY, yellowRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);// 3.2里面的白框
[[UIColor whiteColor] set];
CGFloat whiteRadius = yellowRadius 0.9; // 眼睛大小
CGFloat whiteX = kuangX;
CGFloat whiteY = kuangY;
CGContextAddArc(ctx, whiteX, whiteY, whiteRadius, 0, M_PI 2 , 0);CGContextFillPath(ctx);
// 4.在里面的眼框
[BMColor(108, 81, 55) set];
CGFloat eyeRadius = whiteRadius 0.48;
CGFloat eyeX = kuangX ;
CGFloat eyeY = kuangY + 5; // 眼眶下移.
CGContextAddArc(ctx, eyeX, eyeY, eyeRadius, 0, M_PI 2, 0);
CGContextFillPath(ctx);// 4.1在里面的眼珠
[[UIColor blackColor] set];
CGFloat eyeZhuRadius = eyeRadius 0.55;
CGFloat eyeZHuX = eyeX;
CGFloat eyeZHUY = eyeY + 5; // 眼珠下移.
CGContextAddArc(ctx, eyeZHuX, eyeZHUY, eyeZhuRadius, 0, M_PI 2, 0);
CGContextFillPath(ctx);//4.2 眼白
[[UIColor whiteColor] set];
CGFloat eyeBaiRadius = eyeZhuRadius 0.2;
CGFloat eyeBaiX = eyeZHuX + 6; // 眼白位置控制.
CGFloat eyeBaiY = eyeZHUY + 0; // 眼白位置控制.
CGContextAddArc(ctx, eyeBaiX, eyeBaiY, eyeBaiRadius, 0, M_PI 2, 0);
CGContextFillPath(ctx);//眼皮
[BMColor(252, 198, 0) set];
CGFloat eyePiRadius = whiteRadius;
CGFloat eyePiX = kuangX;
CGFloat eyePiY = kuangY;
CGContextMoveToPoint(ctx, eyePiX - eyePiRadius, eyePiY);
CGFloat contrilEyePiX = eyePiX;
CGFloat contrilEyePiY = eyePiY + 10; // 眼皮大小
CGContextAddQuadCurveToPoint(ctx, contrilEyePiX, contrilEyePiY, eyePiX + eyePiRadius, eyePiY);
CGContextAddArc(ctx, eyePiX, eyePiY, eyePiRadius, 0, M_PI , 1);
CGContextFillPath(ctx);//另一侧眼镜框边框(右)
CGFloat otherEyesBianKuangX = rect.size.width * 0.5 + EyesBianKuangRadius - kuangXPading;
CGFloat otherEyesBianKuangY = BMTopY - eyesBianKuangHight ;
CGContextMoveToPoint(ctx, otherEyesBianKuangX, otherEyesBianKuangY);
CGFloat otherendEyesBianKuangX = otherEyesBianKuangX;
CGFloat otherendEyesBianKuangY = BMTopY + eyesBianKuangHight;
CGContextAddLineToPoint(ctx, otherendEyesBianKuangX, otherendEyesBianKuangY);
CGContextSetLineWidth(ctx, 10); // 绑带线条
CGContextSetLineCap(ctx, kCGLineCapRound);
[BMColor(142, 142, 142) set];
CGContextStrokePath(ctx);// 5.另外一边最外圈的镜框
[BMColor(92, 92, 92) set];
CGFloat OtherKuangY = kuangY;
CGFloat OtherKuangX = rect.size.width 0.5 + kuangRadius - kuangXPading;
CGContextAddArc(ctx, OtherKuangX, OtherKuangY, kuangRadius, 0, M_PI 2, 0);
CGContextFillPath(ctx);// 6.另外一侧里面的皮肤框
[BMColor(252, 198, 0) set];
CGFloat otheryellowX = OtherKuangX;
CGFloat otheryellowY = OtherKuangY;
CGContextAddArc(ctx, otheryellowX, otheryellowY, yellowRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);// 6.2里面的白框
[[UIColor whiteColor] set];
CGFloat otherwhiteX = OtherKuangX;
CGFloat otherwhiteY = OtherKuangY;
CGContextAddArc(ctx, otherwhiteX, otherwhiteY, whiteRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);// 7.在里面的眼框
[BMColor(108, 81, 55) set];
CGFloat otherEyeX = OtherKuangX ;
CGFloat otherEyeY = OtherKuangY + 5; // 眼眶下移
CGContextAddArc(ctx, otherEyeX, otherEyeY, eyeRadius, 0, M_PI 2, 0);
CGContextFillPath(ctx);
// 7.2在里面的眼珠
[[UIColor blackColor] set];
CGFloat othereyeZHuX = otherEyeX;
CGFloat othereyeZHUY = otherEyeY + 5; // 眼珠下移.
CGContextAddArc(ctx, othereyeZHuX, othereyeZHUY, eyeZhuRadius, 0, M_PI 2, 0);
CGContextFillPath(ctx);//7.3 眼白
[[UIColor whiteColor] set];
CGFloat othereyeBaiX = othereyeZHuX - 4;
CGFloat othereyeBaiY = othereyeZHUY + 0; // 眼白控制.
CGContextAddArc(ctx, othereyeBaiX, othereyeBaiY, eyeBaiRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);//另一边眼皮
[BMColor(252, 198, 0) set];
CGFloat otherEyePiRadius = whiteRadius;
CGFloat otherEyePiX = OtherKuangX;
CGFloat otherEyePiY = OtherKuangY;
CGContextMoveToPoint(ctx, otherEyePiX - otherEyePiRadius, otherEyePiY);
CGFloat controlOtherEyePiX = otherEyePiX;
CGFloat controlOtherEyePiY = otherEyePiY + 10; // 眼皮大小
CGContextAddQuadCurveToPoint(ctx, controlOtherEyePiX, controlOtherEyePiY, otherEyePiX + otherEyePiRadius, otherEyePiY);
CGContextAddArc(ctx, otherEyePiX, otherEyePiY, otherEyePiRadius, 0, M_PI , 1);
CGContextFillPath(ctx);
}
/**
画眼镜框边框
*/
void drawEyesBianKuang(CGContextRef ctx, CGRect rect)
{
CGFloat eyesBianKuangHight = 20; // 眼睛边框的高度// 左
CGFloat startX = rect.size.width 0.5 - BMRadius 0.48;
CGFloat startY = BMTopY - eyesBianKuangHight - 5;
CGContextMoveToPoint(ctx, startX, startY);
CGFloat endX = startX;
CGFloat endY = BMTopY + eyesBianKuangHight;
CGContextAddLineToPoint(ctx, endX, endY);
CGContextSetLineWidth(ctx, 10); // 绑带线条
CGContextSetLineCap(ctx, kCGLineCapRound);
[BMColor(234, 234, 234) set];
// 绘制线条
CGContextStrokePath(ctx);// 右
}
/**
画嘴
/
void drawZui(CGContextRef ctx, CGRect rect)
{
// 中间的控制点
CGFloat controlX = rect.size.width 0.5;
CGFloat controlY = rect.size.height * 0.4;// 当前点
CGFloat marginX = 40;
CGFloat marginY = - 18;
CGFloat currentX = controlX - marginX;
CGFloat currentY = controlY - marginY;
CGContextMoveToPoint(ctx, currentX, currentY);// 结束点
CGFloat endX = controlX + marginX;
CGFloat endY = currentY;// 贝塞尔曲线
CGContextAddQuadCurveToPoint(ctx, controlX, controlY, endX, endY);// 设置颜色
[[UIColor blackColor] set];// 渲染
CGContextStrokePath(ctx);
}
/**
画身体
/
void drawBody(CGContextRef ctx, CGRect rect)
{
// 上半圆
CGFloat topX = rect.size.width 0.5;
CGFloat topY = BMTopY;
CGFloat topRadius = BMRadius;
CGContextAddArc(ctx, topX, topY, topRadius, 0, M_PI, 1);// 向下延伸
CGFloat middleX = topX - topRadius;
CGFloat middleH = 200; // 中间身体的高度
CGFloat middleY = topY + middleH;
CGContextAddLineToPoint(ctx, middleX, middleY);// 下半圆
CGFloat bottomX = topX;
CGFloat bottomY = middleY;
CGFloat bottomRadius = topRadius;
CGContextAddArc(ctx, bottomX, bottomY, bottomRadius, M_PI, 0, 1);// 合并路径
CGContextClosePath(ctx);// 设置颜色
[BMColor(252, 218, 0) set];// 利用填充方式画出之前的路径
CGContextFillPath(ctx);
}
代码2.转载示例代码:
#define PI M_PI
#import “allView.h”
#import <QuartzCore/QuartzCore.h>
@implementation allView
(void)drawRect:(CGRect)rect {
//An opaque type that represents a Quartz 2D drawing environment.
//一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
CGContextRef context = UIGraphicsGetCurrentContext();/写文字/
[@”画圆:” drawInRect:CGRectMake(10, 20, 80, 20) withAttributes:nil];
[@”画线及孤线:” drawInRect:CGRectMake(10, 80, 100, 20) withAttributes:nil];;
[@”画矩形:” drawInRect:CGRectMake(10, 120, 80, 20) withAttributes:nil];;
[@”画扇形和椭圆:” drawInRect:CGRectMake(10, 160, 110, 20) withAttributes:nil];;
[@”画三角形:” drawInRect:CGRectMake(10, 220, 80, 20) withAttributes:nil];;
[@”画圆角矩形:” drawInRect:CGRectMake(10, 260, 100, 20) withAttributes:nil];;
[@”画贝塞尔曲线:” drawInRect:CGRectMake(10, 300, 100, 20) withAttributes:nil];;
[@”图片:” drawInRect:CGRectMake(10, 340, 80, 20) withAttributes:nil];/画圆/
//边框圆
CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色
CGContextSetLineWidth(context, 1.0);//线的宽度
//void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。
CGContextAddArc(context, 100, 20, 15, 0, 2*PI , 0); //添加一个圆
CGContextDrawPath(context, kCGPathStroke); //绘制路径//填充圆,无边框
CGContextAddArc(context, 150, 30, 30, 0, 2*PI, 0); //添加一个圆
CGContextDrawPath(context, kCGPathFill);//绘制填充//画大圆并填充颜
UIColoraColor = [UIColor colorWithRed:1 green:0.0 blue:0 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
CGContextSetLineWidth(context, 3.0);//线的宽度
CGContextAddArc(context, 250, 40, 40, 0, 2PI, 0); //添加一个圆
//kCGPathFill填充非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathStroke路径,kCGPathFillStroke路径填充,kCGPathEOFillStroke表示描线,不是填充
CGContextDrawPath(context, kCGPathFillStroke); //绘制路径加填充/画线及孤线/
//画线
CGPoint aPoints[2];//坐标点
aPoints[0] =CGPointMake(100, 80);//坐标1
aPoints[1] =CGPointMake(130, 80);//坐标2
//CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
//points[]坐标数组,和count大小
CGContextAddLines(context, aPoints, 2);//添加线
CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径//画笑脸弧线
//左
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);//改变画笔颜色
CGContextMoveToPoint(context, 140, 80);//开始坐标p1
//CGContextAddArcToPoint(CGContextRef c, CGFloat x1, CGFloat y1,CGFloat x2, CGFloat y2, CGFloat radius)
//x1,y1跟p1形成一条线的坐标p2,x2,y2结束坐标跟p3形成一条线的p3,radius半径,注意, 需要算好半径的长度,
CGContextAddArcToPoint(context, 148, 68, 156, 80, 10);
CGContextStrokePath(context);//绘画路径//右
CGContextMoveToPoint(context, 160, 80);//开始坐标p1
//CGContextAddArcToPoint(CGContextRef c, CGFloat x1, CGFloat y1,CGFloat x2, CGFloat y2, CGFloat radius)
//x1,y1跟p1形成一条线的坐标p2,x2,y2结束坐标跟p3形成一条线的p3,radius半径,注意, 需要算好半径的长度,
CGContextAddArcToPoint(context, 168, 68, 176, 80, 10);
CGContextStrokePath(context);//绘画路径//右
CGContextMoveToPoint(context, 150, 90);//开始坐标p1
//CGContextAddArcToPoint(CGContextRef c, CGFloat x1, CGFloat y1,CGFloat x2, CGFloat y2, CGFloat radius)
//x1,y1跟p1形成一条线的坐标p2,x2,y2结束坐标跟p3形成一条线的p3,radius半径,注意, 需要算好半径的长度,
CGContextAddArcToPoint(context, 158, 102, 166, 90, 10);
CGContextStrokePath(context);//绘画路径
//注,如果还是没弄明白怎么回事,请参考:http://donbe.blog.163.com/blog/static/138048021201052093633776//画矩形/
CGContextStrokeRect(context,CGRectMake(100, 120, 10, 10));//画方框
CGContextFillRect(context,CGRectMake(120, 120, 10, 10));//填充框
//矩形,并填弃颜色
CGContextSetLineWidth(context, 2.0);//线的宽度
aColor = [UIColor blueColor];//blue蓝色
CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
aColor = [UIColor yellowColor];
CGContextSetStrokeColorWithColor(context, aColor.CGColor);//线框颜色
CGContextAddRect(context,CGRectMake(140, 120, 60, 30));//画方框
CGContextDrawPath(context, kCGPathFillStroke);//绘画路径//矩形,并填弃渐变颜色
//关于颜色参考http://blog.sina.com.cn/s/blog_6ec3c9ce01015v3c.html
//http://blog.csdn.net/reylen/article/details/8622932
//第一种填充方式,第一种方式必须导入类库quartcore并#import <QuartzCore/QuartzCore.h>,这个就不属于在context上画,而是将层插入到view层上面。那么这里就设计到Quartz Core 图层编程了。
CAGradientLayer *gradient1 = [CAGradientLayer layer];
gradient1.frame = CGRectMake(240, 120, 60, 30);
gradient1.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor grayColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor brownColor].CGColor,nil];
[self.layer insertSublayer:gradient1 atIndex:0];
//第二种填充方式
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{1,1,1, 1.00, 1,1,0, 1.00, 1,0,0, 1.00, 1,0,1, 1.00, 0,1,1, 1.00, 0,1,0, 1.00, 0,0,1, 1.00, 0,0,0, 1.00,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents
(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])4));//形成梯形,渐变的效果
CGColorSpaceRelease(rgb);
//画线形成一个矩形
//CGContextSaveGState与CGContextRestoreGState的作用
/
CGContextSaveGState函数的作用是将当前图形状态推入堆栈。之后,您对图形状态所做的修改会影响随后的描画操作,但不影响存储在堆栈中的拷贝。在修改完成后,您可以通过CGContextRestoreGState函数把堆栈顶部的状态弹出,返回到之前的图形状态。这种推入和弹出的方式是回到之前图形状态的快速方法,避免逐个撤消所有的状态修改;这也是将某些状态(比如裁剪路径)恢复到原有设置的唯一方式。
*/
CGContextSaveGState(context);
CGContextMoveToPoint(context, 220, 90);
CGContextAddLineToPoint(context, 240, 90);
CGContextAddLineToPoint(context, 240, 110);
CGContextAddLineToPoint(context, 220, 110);
CGContextClip(context);//context裁剪路径,后续操作的路径
//CGContextDrawLinearGradient(CGContextRef context,CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,CGGradientDrawingOptions options)
//gradient渐变颜色,startPoint开始渐变的起始位置,endPoint结束坐标,options开始坐标之前or开始之后开始渐变
CGContextDrawLinearGradient(context, gradient,CGPointMake(220,90) ,CGPointMake(240,110), kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);// 恢复到之前的context
//再写一个看看效果
CGContextSaveGState(context);
CGContextMoveToPoint(context, 260, 90);
CGContextAddLineToPoint(context, 280, 90);
CGContextAddLineToPoint(context, 280, 100);
CGContextAddLineToPoint(context, 260, 100);
CGContextClip(context);//裁剪路径
//说白了,开始坐标和结束坐标是控制渐变的方向和形状
CGContextDrawLinearGradient(context, gradient,CGPointMake(260, 90) ,CGPointMake(260, 100), kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);// 恢复到之前的context
//下面再看一个颜色渐变的圆
CGContextDrawRadialGradient(context, gradient, CGPointMake(300, 100), 0.0, CGPointMake(300, 100), 10, kCGGradientDrawsBeforeStartLocation);/画扇形和椭圆/
//画扇形,也就画圆,只不过是设置角度的大小,形成一个扇形
aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
//以10为半径围绕圆心画指定角度扇形
CGContextMoveToPoint(context, 160, 180);
CGContextAddArc(context, 160, 180, 30, -60 PI / 180, -120 PI / 180, 1);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke); //绘制路径//画椭圆
CGContextAddEllipseInRect(context, CGRectMake(160, 180, 20, 8)); //椭圆
CGContextDrawPath(context, kCGPathFillStroke);/画三角形/
//只要三个点就行跟画一条线方式一样,把三点连接起来
CGPoint sPoints[3];//坐标点
sPoints[0] =CGPointMake(100, 220);//坐标1
sPoints[1] =CGPointMake(130, 220);//坐标2
sPoints[2] =CGPointMake(130, 160);//坐标3
CGContextAddLines(context, sPoints, 3);//添加线
CGContextClosePath(context);//封起来
CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径/画圆角矩形/
float fw = 180;
float fh = 280;CGContextMoveToPoint(context, fw, fh-20); // 开始坐标右边开始
CGContextAddArcToPoint(context, fw, fh, fw-20, fh, 10); // 右下角角度
CGContextAddArcToPoint(context, 120, fh, 120, fh-20, 10); // 左下角角度
CGContextAddArcToPoint(context, 120, 250, fw-20, 250, 10); // 左上角
CGContextAddArcToPoint(context, fw, 250, fw, fh-20, 10); // 右上角
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径/画贝塞尔曲线/
//二次曲线
CGContextMoveToPoint(context, 120, 300);//设置Path的起点
CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);//设置贝塞尔曲线的控制点坐标和终点坐标
CGContextStrokePath(context);
//三次曲线函数
CGContextMoveToPoint(context, 200, 300);//设置Path的起点
CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);//设置贝塞尔曲线的控制点坐标和控制点坐标终点坐标
CGContextStrokePath(context);/图片/
UIImage *image = [UIImage imageNamed:@”IMG_0291”];
[image drawInRect:CGRectMake(60, 340, 50, 50)];//在坐标中画出图片
// [image drawAtPoint:CGPointMake(100, 340)];//保持图片大小在point点开始画图片,可以把注释去掉看看
CGContextDrawImage(context, CGRectMake(60, 400, 50, 50), image.CGImage);//使用这个使图片上下颠倒了,参考http://blog.csdn.net/koupoo/article/details/8670024// CGContextDrawTiledImage(context, CGRectMake(0, 0, 20, 20), image.CGImage);//平铺图
}
原文出处:http://blog.csdn.net/rhljiayou/article/details/9919713