//利用CG框架绘图的步骤:
1.子类化UIView
2.在GraphicView中复写drawRect方法
3.获取图形上下文对象
4.绘制线条
(1)先创建一个路径,把线条定义在这个路径上,再把路径交给context去显示。
(2)直接把线条绘制到context上去。
5.绘制简单图形
6.绘制文本
7.绘制图像
- (void)drawRect:(CGRect)rect {
//1.获取图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();//图形上下文
[self drawImage:context];
}
//绘制线条方法一
//创建一个绘制路径
CGMutablePathRef path =CGPathCreateMutable();
//2.在路径上添加一个起点
CGPathMoveToPoint(path,NULL, 50, 50);
CGPathAddLineToPoint(path,NULL, 100, 100);
//绘制完毕后关闭路径,这样会把起始点和终点连接
CGPathCloseSubpath(path);
//3.把路径添加到图形上下文
CGContextAddPath(context,path);
//4.设置图形上下文的属性
//设置线宽
CGContextSetLineWidth(context, 5);
//设置线条颜色
CGContextSetRGBStrokeColor(context, 254.0/255, 201.0/255, 21.0/255, 1);
//填充颜色
CGContextSetRGBFillColor(context, 0, 1, 0, 1);
//5.在图形上下文上绘制路径
/*
绘制模式:
kCGPathFill:填充(实心)
kCGPathStroke:画线(空心)
kCGPathFillStroke :即画线又填充
*/
CGContextDrawPath(context,kCGPathFillStroke);
//6.释放路径,有create就要有release
CGPathRelease(path);
//绘制线条方法二
CGPoint points[] = {{50,50},{100,100},{50,100},{50,50}};
CGContextAddLines(context,points, 4);
//设置线条颜色
[[UIColor redColor]setStroke];
//设置填充颜色
[[UIColor blueColor]setFill];
//同时设置线条和填充颜色
[[UIColor redColor] set];
CGContextDrawPath(context, kCGPathFillStroke);
//绘制矩形
//1.使用CG提供的函数
CGRect rect =CGRectMake(10, 10, 100, 200);
//添加到图形上下文中
CGContextAddRect(context,rect);
//设置context的属性
[[UIColor redColor]set];
CGContextDrawPath(context,kCGPathFillStroke);
//2.使用UIKit框架提供的函数
CGRect rect =CGRectMake(10, 10, 100, 200);
//设置context的属性
[[UIColor redColor] set];
//绘制实心矩形
//UIRectFill(rect);
//绘制空心矩形
UIRectFrame(rect);
//绘制一个圆形
x,y圆心的坐标
radius:半径
startAngel:起始角度
endAngel:结束的角度
clockwise:0顺时针,1逆时针
CGContextAddArc(context,160, 160, 100, 0, M_PI_4, 0);
//2.给定一个矩形,绘制一个内切圆
CGRect rect =CGRectMake(50, 50, 100, 100);
[[UIColor redColor] set];
UIRectFrame(rect);
CGContextAddEllipseInRect(context, rect);
CGContextDrawPath(context,kCGPathFillStroke);
//绘制贝赛尔曲线
//设置起点
CGContextMoveToPoint(context, 20, 200);
//CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);
CGContextAddQuadCurveToPoint(context, 150, 20, 300, 200);
[[UIColor redColor] set];
CGContextDrawPath(context,kCGPathStroke);
//绘制一个文本
NSString *str =@"hello world";
CGRect rect =CGRectMake(50, 50, 200, 300);
[[UIColor whiteColor]setFill];
UIRectFill(rect);
UIFont *font = [UIFontsystemFontOfSize:20];
UIColor *color = [UIColorredColor];
//创建段落样式对象,通过对其属性的设置可以设置文字在段落中显示的样式。
NSMutableParagraphStyle*style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode =NSLineBreakByWordWrapping;
style.alignment =NSTextAlignmentCenter;
// [str drawInRect:rectwithFont:font lineBreakMode:NSLineBreakByWordWrappingalignment:NSTextAlignmentCenter];
[str drawInRect:rectwithAttributes:
@{
NSFontAttributeName:font,
NSForegroundColorAttributeName:color,
NSParagraphStyleAttributeName:style
}];
//这种绘制文本的方式非常轻量级,不需要创建一个UILabel控件,并且可以根据需求定制效果。
//绘制图片
//1.使用UIKit提供的方法绘制
UIImage *image = [UIImageimageNamed:@"mp3.jpg"];
//图片拉伸在指定矩形区域显示
[imagedrawInRect:CGRectMake(0, 0, 320, 200)];
//图片平铺在指定区域显示
//[imagedrawAsPatternInRect:CGRectMake(0, 0, 320, 200)];
//2.用CoreGraphic提供的函数绘制
//CG坐标系统转化为UIKit的坐标系统
CGContextSaveGState(context);
//(1)顺时针旋转180度
CGContextRotateCTM(context, M_PI);
//(2)x缩放为原来的-1倍
CGContextScaleCTM(context,-1, 1);
//(3)向上平移
CGContextTranslateCTM(context, 0,-image.size.height);
CGContextDrawImage(context, CGRectMake(0,0, image.size.width, image.size.height), image.CGImage);
//(4)恢复context
CGContextRestoreGState(context);