标签:
- (void)drawRect:(CGRect)rect
{
// 获取context
CGContextRef context = UIGraphicsGetCurrentContext();
// 画直线
CGContextMoveToPoint(context, 50, 100);
CGContextAddLineToPoint(context, 100, 300);
CGContextSetLineWidth(context, 5);
CGContextSetLineCap(context, kCGLineCapRound);
[[UIColor redColor] set];
CGContextAddLineToPoint(context, 200, 20);
CGContextStrokePath(context);
// 三角形
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 10, 100);
CGContextAddLineToPoint(context, 200, 100);
[[UIColor grayColor]set];
CGContextSetLineWidth(context, 2);
CGContextClosePath(context);
CGContextStrokePath(context);
// 画四边形
// CGContextAddRect(context, CGRectMake(100, 100, 200, 50));
// CGContextStrokePath(context);
CGContextFillRect(context, CGRectMake(100, 100, 200, 50));
// 画圆
CGContextAddArc(context, 250, 250, 50, 0, 2*M_PI, 0);
CGContextFillPath(context);
// CGContextStrokePath(context);
// 画椭圆
CGContextAddEllipseInRect(context, CGRectMake(300, 200, 100, 200));
// CGContextStrokePath(context);
CGContextFillPath(context);
// 画圆弧
CGContextAddArc(context, 100, 400, 50, 0, 0.5*M_PI, 0);
CGContextClosePath(context);
// CGContextStrokePath(context);
CGContextFillPath(context);
// 2.画饼状图 --------------------
// 画线
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 100, 150);
// 画圆弧
CGContextAddArc(context, 100, 100, 50, M_PI_2, M_PI, 0);
// CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);
// 关闭路径
CGContextClosePath(context);
[[UIColor brownColor]set];
// 3.渲染 (注意, 画线只能通过空心来画)
CGContextFillPath(context);
// 图形上下文栈 保存:CGContextSaveGState(context); 恢复:CGContextRestoreGState(context); 在栈里保存了几次,那么就可以取几次(比如不能保存了1次,取两次,在取第二次的时候,栈里为空会直接挂掉)
// 第一条线
CGContextMoveToPoint(context, 50, 700);
CGContextAddLineToPoint(context, 50, 800);
[[UIColor redColor]set];
CGContextSaveGState(context);
CGContextStrokePath(context);
// 第二条线
CGContextMoveToPoint(context, 100, 700);
CGContextAddLineToPoint(context, 200, 800);
[[UIColor grayColor]set];
CGContextStrokePath(context);
// 第三条线,用第一条线的样式
CGContextMoveToPoint(context, 150, 800);
CGContextAddLineToPoint(context, 180, 920);
CGContextRestoreGState(context);
CGContextStrokePath(context);
// 第四条线,用第一条线的样式, 只保存了一次,但是第二次用,崩溃....
CGContextMoveToPoint(context, 220, 800);
CGContextAddLineToPoint(context, 180, 920);
// CGContextRestoreGState(context);
CGContextStrokePath(context);
// 旋转角度
CGContextRotateCTM(context, M_PI_4);
CGContextAddRect(context, CGRectMake(200, 200, 500, 100));
CGContextStrokePath(context);
// 缩放
CGContextScaleCTM(context, 2, 0.5);
CGContextAddRect(context, CGRectMake(100, 100, 200, 200));
CGContextStrokePath(context);
// 平移
CGContextTranslateCTM(context, -100, 100);
CGContextAddRect(context, CGRectMake(100, 100, 200, 200));
CGContextStrokePath(context);
}
标签:
原文地址:http://www.cnblogs.com/xiangjune/p/4969460.html