利用Quartz2D可以绘制出系统不能提供的各种图形, 比如饼状图, 柱状图, 以及各种曲线, 所以熟练使用Quartz2D绘图还是有必要的, 下面是绘制基本图形
// Quartz2D基本图形绘制
//
// Created by Wangjunling on 16/3/9.
// Copyright © 2016年 Wangjunling. All rights reserved.
//
#import "WJLView.h"
@implementation WJLView
- (void)drawRect:(CGRect)rect {
// [self test1];//画线
// [self test2];//画矩形
[self test6];
}
- (void)test1 { //画线
//获取当前绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置起点
CGContextMoveToPoint(ctx, 100, 100);
//设置下一个点
CGContextAddLineToPoint(ctx, 100, 200);
CGContextAddLineToPoint(ctx, 200, 200);
//设置线宽
CGContextSetLineWidth(ctx, 10);
//设置线头的样式(圆头)
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置两条线连接处的样式
CGContextSetLineJoin(ctx, kCGLineJoinMiter);
//绘制图形
CGContextStrokePath(ctx);
}
- (void)test2 { //画矩形
//获取当前绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddRect(ctx, CGRectMake(0, 0, 200, 200));
//绘制图形
CGContextStrokePath(ctx);
//填充模式绘制
//
CGContextFillPath(ctx);
}
- (void)test3 { //画圆形
//获取当前绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//方式一
CGContextAddEllipseInRect(ctx, CGRectMake(0, 50, 300, 200));
//方式二;
CGContextAddArc(ctx, 150, 150, 50, 0, M_PI*2, 1);
//绘制图形
CGContextStrokePath(ctx);
}
- (void)test4 {//扇形
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置起点为圆心
CGContextMoveToPoint(ctx, 150, 150);
CGContextAddArc(ctx, 150, 150, 100, 0, M_PI_4, 0);
//封闭路径
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
}
- (void)test5 {//画同心圆
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddArc(ctx, 150, 150, 50, 0, M_PI*2, 0);
//移除两个圆的链接线
CGContextMoveToPoint(ctx, 250, 150);
CGContextAddArc(ctx, 150, 150, 100, 0, M_PI*2, 0);
}
- (void)test6 {//虚线
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 0, 100);
CGContextAddLineToPoint(ctx, 300, 100);
//设置空实比例, 第一个参数为第一段的
CGFloat lengths[4] = {10, 5, 10, 5};
CGContextSetLineDash(ctx, 0, lengths, 4 );
CGContextStrokePath(ctx);
}
- (void)test7 {//贝塞尔曲线
CGContextRef ctx = UIGraphicsGetCurrentContext();
//起始点
CGContextMoveToPoint(ctx, 50, 200);
//控制点和结束点
CGContextAddQuadCurveToPoint(ctx, 150, 50, 250, 200);
CGContextStrokePath(ctx);
}