void test2()
{
//绘制矩形
CGContextRef context = UIGraphicsGetCurrentContext(); //获得上下文
CGContextAddRect(context, CGRectMake(50, 100, 100, 50));
[[UIColor blueColor] setStroke];
// [[UIColor brownColor] set]; //设置线条颜色
CGContextStrokePath(context); //合并路径
// CGContextFillPath(context); //填充
}
/**
* 基本线条的绘制
*/
void test()
{
// 如果是在drawRect方法中调用UIGraphicsGetCurrentContext方法获取出来的就是Layer的上下文
// 1.创建画布
CGContextRef context = UIGraphicsGetCurrentContext(); //相当于绘画布
// 2。设置绘画起点
CGContextMoveToPoint(context, 10, 30);
// 3、设置终点
// CGContextAddLineToPoint 绘制线
CGContextAddLineToPoint(context, 100,100);
//设置线条的相关属性
// CGContextSetLineCap(context, kCGLineCapRound); //线条末尾结束样式
CGContextSetLineWidth(context, 10); //设置线宽
CGContextSetLineJoin(context, kCGLineJoinBevel); //设置拐角样式
// CGContextSetRGBStrokeColor(context, 10, 100, 19, 1); //设直线条颜色
// 4、合并路径
CGContextStrokePath(context);
//////////////////////////////////////////
CGContextMoveToPoint(context, 100, 30); //绘制第二条线的起点
CGContextAddLineToPoint(context, 300, 50);// 设置第二条直线的终点(自动把上一条直线的终点当做起点)
//快速设置线条颜色
[[UIColor brownColor] set];
CGContextStrokePath(context);
//
}
/**
* 绘制三角形
*/
void test3()
{
CGContextRef context = UIGraphicsGetCurrentContext(); //相当于绘画布
// 设置起点
CGContextMoveToPoint(context, 40,40);
//设置第二个起点
CGContextAddLineToPoint(context, 40, 100);
//设置第二条线的终点,自动会以上一条线的末尾当做起点
CGContextAddLineToPoint(context, 100, 100);
// 设置终点
CGContextAddLineToPoint(context, 40, 40);
// CGContextClosePath(context); //关闭路径
//渲染到图形上
CGContextStrokePath(context);
}
//绘制圆形
void test4()
{
CGContextRef context = UIGraphicsGetCurrentContext();
/**
* 绘制弧形
*
* @param x#> x 圆心的x
* @param y#> y 圆心的y
* @param radius#> radius
* @param startAngle#> startAngle 起始角度
* @param endAngle#> endAngle 结束角度
* @param clockwise#> clockwise 0顺时针 1逆时针
*
*/
CGContextAddArc(context, 100, 100, 50, 0, M_PI * 2, 1);
// 3.渲染 (注意, 画线只能通过空心来画)
// CGContextFillPath(context);
CGContextStrokePath(context);
}
/**
* 绘制饼状图
*/
void test5()
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画饼状图
// 画线
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 100, 150);
// 画圆弧
CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
// CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);
// 关闭路径
CGContextClosePath(ctx);
// 3.渲染 (注意, 画线只能通过空心来画)
CGContextFillPath(ctx);
// CGContextStrokePath(ctx);
}
/**
* 绘制圆弧
*/
void test6()
{
// 画圆弧
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画圆弧
// x/y 圆心
// radius 半径
// startAngle 开始的弧度
// endAngle 结束的弧度
// clockwise 画圆弧的方向 (0 顺时针, 1 逆时针)
// CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI_2, 0);
CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
// CGContextClosePath(ctx);
// 3.渲染
CGContextStrokePath(ctx);
// CGContextFillPath(ctx);
}
/**
* 绘制椭圆
*/
void test7()
{
// 画圆
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 50));
[[UIColor greenColor] set];
// 3.渲染
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
}
/**
* 图片的绘制
*/
void test8()
{
UIImage *image = [UIImage imageNamed:@"backGroundImage"];
// 利用OC方法将图片绘制到layer上
// 将图片绘制到指定的位置
// [image drawAtPoint:CGPointMake(100, 0)];
// 利用drawInRect方法绘制图片到layer, 是通过拉伸原有图片
// [image drawInRect:CGRectMake(40, 40, 100, 100)];
// 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
[image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}
/**
* 画文字
*/
void test9()
{
// 画文字
NSString *str = @"天气好热地方和计算机的开了房间了开始的解放路口时间疯狂的老师;快疯了;SD卡;焚枯食淡;李开复;顺丰快递说了;开发;拉伸放假快乐的设计风格看了就打算离开房间的数量会计分录开始觉得";
// 1.获取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.绘图
// 不推荐使用C语言的方法绘制文字, 因为quraz2d中的坐标系和UIkit中的坐标系不一致, 绘制出来的文字是颠倒的, 而且通过C语言的方法绘制文字相当麻烦
// CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)
// CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)
// 绘制矩形
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.绘图
CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));
// 3.渲染
CGContextStrokePath(ctx);
NSMutableDictionary *md = [NSMutableDictionary dictionary];
// 设置文字颜色
md[NSForegroundColorAttributeName] =[UIColor redColor];
// 设置文字背景颜色
md[NSBackgroundColorAttributeName] = [UIColor greenColor];
// 设置文字大小
md[NSFontAttributeName] = [UIFont systemFontOfSize:20];
// 将文字绘制到指点的位置
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
// 将文字绘制到指定的范围内, 如果一行装不下会自动换行, 当文字超出范围后就不显示
[str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];
}
/**
* 图形上下文栈
*/
void test10()
{
// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 保存一份最纯洁的图形上下文
// 调用一次该方法就会拷贝一个上下文到栈中
CGContextSaveGState(ctx);
//CGContextSaveGState(ctx);
// 第一条线
// 利用图形上下文保存绘图信息
CGContextMoveToPoint(ctx, 150, 20);
CGContextAddLineToPoint(ctx, 20, 100);
// 设置第一条线的状态
CGContextSetLineWidth(ctx, 10);
CGContextSetLineCap(ctx, kCGLineCapRound);
[[UIColor redColor] set];
// 渲染
CGContextStrokePath(ctx);
// 还原开始保存的那份最纯洁的图形上下文
CGContextRestoreGState(ctx);
// 第二条线
CGContextMoveToPoint(ctx, 80, 30);
CGContextAddLineToPoint(ctx, 80, 150);
// 渲染
CGContextStrokePath(ctx);
/*
// 清空状态
CGContextSetLineWidth(ctx, 5);
CGContextSetLineCap(ctx, kCGLineCapButt);
[[UIColor greenColor] set];
*/
}
// 当自定义view第一次显示出来的时候就会调用drawRect方法
- (void)drawRect:(CGRect)rect
{
test10();
}