上一篇博客已经介绍了Quartz2d最基本的使用,那么本文会继续深入的解析Quartz2d。
1.绘制圆
/**
* 绘制圆弧
*/
void drawCircle()
{
//1.获得上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//2.绘制圆
/**
* 颜色设置的方法,下面三种方式选择一个
*/
//面向对象方法设置颜色
//设置空心的颜色
[[UIColor blueColor] setStroke];
//设置实心的颜色
[[UIColor blueColor] setFill];
//同时设置空心和实心的颜色
[[UIColor blueColor] set];
//用quartz2d的c语言方法设置空心颜色,下面两种方式选择一个
// CGContextSetRGBStrokeColor(ref, <#CGFloat red#>, <#CGFloat green#>, <#CGFloat blue#>, <#CGFloat alpha#>)
//用用quartz2d的c语言方法设置实心颜色
// CGContextSetRGBFillColor(<#CGContextRef context#>, <#CGFloat red#>, <#CGFloat green#>, <#CGFloat blue#>, <#CGFloat alpha#>)
//设置线的宽度
CGContextSetLineWidth(ref, 10);
//添加椭圆(如果长度和宽度相等,那么就是圆)
CGContextAddEllipseInRect(ref, CGRectMake(10, 10, 120, 120));
//3.显示所绘制的东西
CGContextStrokePath(ref);
}
2绘制圆弧
/**
* 画圆弧
*/
void drawArc()
{
CGContextRef ref = UIGraphicsGetCurrentContext();
//画圆弧
//CGContextRef c: 你的上下文
//CGFloat x : 圆心的x坐标
//CGFloat x : 圆心的y坐标
//CGFloat radius: 半径长度
//CGFloat startAngle: 起点到默认0°线的圆心角
//CGFloat endAngle: 终点到默认0°线的圆心角
//int clockwise : 顺时针(0)还是逆时针(1)旋转
// CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
CGContextAddArc(ref, 100, 100, 50, 0, M_PI, 1);
//设置线段宽度
CGContextSetLineWidth(ref, 10);
CGContextStrokePath(ref);
}
3.绘制文字
/**
* 画文字
*/
void drawText()
{
//需要注意的是用面向对象的方式绘制文字,不需要上下文和显示出来那两个步骤!
NSString * str = @"我是文字";
//从一个点写文字
// [str drawAtPoint:CGPointZero withAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont systemFontOfSize:50]}];
//从一个范围呢写文字(字典内写的值是颜色和大小)
[str drawInRect:CGRectMake(0, 0, 200, 200) withAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont systemFontOfSize:50]}];
}
4绘制图片
/**
* 画图片
*/
void drawImage()
{
//需要注意的是用面向对象的方式绘制图片,不需要上下文和显示出来那两个步骤!
UIImage * image = [UIImage imageNamed:@"屏幕快照 2015-08-20 下午2.04.46"];
//以一个点开始展示图片
//[image drawAtPoint:CGPointZero];
//以一个范围自动拉伸图片
[image drawInRect:CGRectMake(0, 0, 200, 200)];
//平铺图片,如果图片小于范围会出现几个图片
[image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];
}
注意,上面有三种绘制图片的方式,三种方式不一样。请大家动手自己尝试。