1.what about Quartz2D ?
1.1简介
1.2图形上下文
(输出目标可以是PDF文件、Bitmap或者显示器的窗口上)
1.3 how to use it 自定义view
1.3.1drawRect:
Quartz2D须知
CGContextRef ctx= UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx,10,10);
CGContextAddLineToPoint(ctx,100,100);
CGContextStrokePath(ctx); //CGContextFillPath(ctx);
常用拼接路径函数
void CGContextMoveToPoint(CGContextRef c,CGFloat x,CGFloat y)
void CGContextAddLineToPoint(CGContextRef c,CGFloat x,CGFloat y)
void CGContextAddRect(CGContextRef c,CGRect rect)
void CGContextAddEllipseInRect(CGContextRef context,CGRect rect)
void CGContextAddArc(CGContextRef c,CGFloat x,CGFloat y,
CGFloat radius, CGFloat startAngle, CGFloat endAngle, intclockwise)
void CGContextDrawPath(CGContextRef c,CGPathDrawingMode mode)
void CGContextStrokePath(CGContextRef c)
void CGContextFillPath(CGContextRef c)
提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的
1.3.2图形上下文栈的操作
void CGContextSaveGState(CGContextRef c)
void CGContextRestoreGState(CGContextRef c)
2.案例 基本图形绘制
2.1运行效果如下
2.2.实现步骤
2.2.1新建一个project,然后自定义一个view
2.2.2代码
#import "htingLineView.h"
@implementation htingLineView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
// 1.获得图形上下文 (c语言必须取得上下文 oc则不用) typedef struct CGContext *CGContextRef;
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接图形(路径)
// 设置线段宽度(一定要在渲染的前面否则没有效果)
CGContextSetLineWidth(ctx, 10);
// 设置线段头尾部的样式
CGContextSetLineCap(ctx, kCGLineCapRound);
// 设置线段转折点的样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);
/** 第1根线段 **/
// 设置颜色
CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
// 设置一个起点
CGContextMoveToPoint(ctx, 10, 10);
// 添加一条线段到(100, 100)
CGContextAddLineToPoint(ctx, 100, 100);
// 渲染一次(如果这里不渲染那么上面设置的颜色不会起效果,只会显示下面渲染的颜色,如果要设置颜色则必须每设置一次就渲染一次)
CGContextStrokePath(ctx); //CGContextStrokePath这个方法是把路径以空心的形式显示出来
/** 第2根线段 **/
// 设置颜色
CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);
// 设置一个起点
CGContextMoveToPoint(ctx, 200, 190);
// 添加一条线段到(150, 40)
CGContextAddLineToPoint(ctx, 150, 40);
CGContextAddLineToPoint(ctx, 120, 60);
// 3.渲染显示到view上面
CGContextStrokePath(ctx);
}
@end