以下是具体代码:
-(void)drawline
{
// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//起点
CGContextMoveToPoint(ctx, 10, 20);
//终点
CGContextAddLineToPoint(ctx, 100, 300);
//线宽
CGContextSetLineWidth(ctx, 20);
[[UIColor greenColor] set];
//渲染
CGContextStrokePath(ctx);
//获取上下文
CGContextRef a = UIGraphicsGetCurrentContext();
//起点
CGContextMoveToPoint(a, 10, 300);
//终点
CGContextAddLineToPoint(a, 100, 20);
//线宽
CGContextSetLineWidth(a, 20);
[[UIColor greenColor] set];
//渲染
CGContextStrokePath(a);
// CGContextRef b = UIGraphicsGetCurrentContext();
// //创建一条直线绘制的路径
// //注意::凡是通过Quartz2D中带有create/copy/retain方法创建出来的值都必须要释放
// CGMutablePathRef path = CGPathCreateMutable();
// //把绘图信息添加到路径里;
// //起点
// CGPathMoveToPoint(path, NULL, 60, 60);
// //终点
// CGPathAddLineToPoint(path, NULL, 200, 200);
// //线宽
// CGContextSetLineWidth(b, 10);
// //着色
// [[UIColor redColor] set];
// //把路径添加到上下文中
// //把绘制直线的的绘图信息保存到图形上下文中
// CGContextAddPath(b, path);
// //渲染
// CGContextStrokePath(b);
/**以上两种绘图(直线的方式)优缺点:第一种代码阅读性不好,不便于区分,第二种 在使用path时,一个path代表一条路径‘。比如如果要在上下文中绘制多个图形,这种情况下建议使用path*/
}
//直线
-(void)drawtu
{//updowncontext
CGContextRef c = UIGraphicsGetCurrentContext();
//start
CGContextMoveToPoint(c, 20, 20);
//last
CGContextAddLineToPoint(c, 20, 100);
CGContextAddLineToPoint(c, 100, 20);
//close
CGContextClosePath(c);
CGContextSetLineWidth(c, 2);
//fillcolour
[[UIColor redColor] setFill];
CGContextDrawPath(c, kCGPathFillStroke);
}
//矩形
-(void)drawjuxing
{
CGContextRef d = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(d, 30, 40);
CGContextAddLineToPoint(d, 30, 100);
CGContextAddLineToPoint(d, 100, 100);
CGContextAddLineToPoint(d, 100, 40);
CGContextClosePath(d);
CGContextSetLineWidth(d, 3);
[[UIColor greenColor] setFill];
CGContextDrawPath(d, kCGPathFillStroke);
}
//圆
-(void)drawCircle
{
CGContextRef e = UIGraphicsGetCurrentContext();
//参数 上下文,圆点,半径 起始角度,结束角度
CGContextAddArc(e, 70,80 , 50,2*M_PI*3/4, 0, 1);
CGContextSetLineWidth(e, 5);
[[UIColor greenColor] set];
//渲染
CGContextStrokePath(e);
CGContextRef f = UIGraphicsGetCurrentContext();
//参数 上下文,圆点,半径 起始角度,结束角度
CGContextAddArc(f, 70,80 , 50,0,2*M_PI*3/4,1);
CGContextSetLineWidth(f, 5);
[[UIColor blackColor] set];
//渲染
CGContextStrokePath(f);
}
//椭圆
-(void)drawellipse
{
CGContextRef g = UIGraphicsGetCurrentContext();
//
CGRect aRect = CGRectMake(20, 20, 160, 100);
//
CGContextAddEllipseInRect(g, aRect);
//渲染
CGContextStrokePath(g);
}
//曲线
-(void)drawquxian
{
CGContextRef g = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(g, 2);
//
CGContextSetStrokeColorWithColor(g, [UIColor grayColor].CGColor);
CGContextMoveToPoint(g, 80, 150);
//
CGContextAddQuadCurveToPoint(g, 200, 200, 200, 400);
CGContextStrokePath(g);
}
//虚线
-(void)drawxuxian
{
CGContextRef g = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(g, 5);
CGContextSetStrokeColorWithColor(g, [UIColor greenColor].CGColor);
CGFloat a[]= {2,6,4,2};
//
CGContextSetLineDash(g, 3, a, 4);
CGContextMoveToPoint(g, 10, 150);
//
CGContextAddQuadCurveToPoint(g, 150, 150, 300, 200);
CGContextStrokePath(g);
}
//文字
-(void)writechinese
{
CGContextRef g = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(g, 2);
CGContextSetRGBFillColor(g, 0.5, 0.5, 0.5, .5);
UIFont * font = [UIFont boldSystemFontOfSize:18.0];
[@" this is my world"drawInRect:CGRectMake(20, 170, 200, 30) withFont:font];
CGContextStrokePath(g);
}
//椭圆
-(void)draw
{//updown
CGContextRef a = UIGraphicsGetCurrentContext();
CGRect aRect= CGRectMake(80, 100,210 ,90 );
CGContextAddEllipseInRect(a, aRect);
[[UIColor redColor] set];
//x r
CGContextStrokePath(a);
}