配色: 字号:
iOS开发中Quartz2D的基本使用方式举例
2016-12-06 | 阅:  转:  |  分享 
  
iOS开发中Quartz2D的基本使用方式举例

这篇文章主要介绍了iOS开发中Quartz2D的基本使用方式举例,需要的朋友可以参考下

一、画直线



代码:





复制代码代码如下:





//

//YYlineview.m

//03-画直线

//

//Createdbyappleon14-6-9.

//Copyright(c)2014年itcase.Allrightsreserved.

//



#import"YYlineview.h"



@implementationYYlineview





//当自定义view第一次显示出来的时候就会调用drawRect方法

-(void)drawRect:(CGRect)rect

{



//1.取得和当前视图相关联的图形上下文(因为图形上下文决定绘制的输出目标)/

//如果是在drawRect方法中调用UIGraphicsGetCurrentContext方法获取出来的就是Layer的上下文

CGContextRefctx=UIGraphicsGetCurrentContext();//不需要,同id



//2.绘图(绘制直线),保存绘图信息

//设置起点

CGContextMoveToPoint(ctx,20,100);

//设置终点

CGContextAddLineToPoint(ctx,300,100);





//设置绘图的状态

//设置线条的颜色为蓝色

CGContextSetRGBStrokeColor(ctx,0,1.0,0,1.0);

//设置线条的宽度

CGContextSetLineWidth(ctx,15);

//设置线条起点和终点的样式为圆角

CGContextSetLineCap(ctx,kCGLineCapRound);

//设置线条的转角的样式为圆角

CGContextSetLineJoin(ctx,kCGLineJoinRound);

//3.渲染(绘制出一条空心的线)

CGContextStrokePath(ctx);



////注意线条不能渲染为实心的

//CGContextFillPath(ctx);







//设置第二条线

//设置第二条线的起点

CGContextMoveToPoint(ctx,50,200);

//设置第二天线的终点(自动把上一条直线的终点当做起点)

CGContextAddLineToPoint(ctx,300,60);



//设置绘图的状态

//CGContextSetRGBStrokeColor(ctx,1.0,0.7,0.3,1.0);

//第二种设置颜色的方式

[[UIColorgrayColor]set];

//设置线条的宽度

CGContextSetLineWidth(ctx,10);

//设置线条的起点和终点的样式

CGContextSetLineCap(ctx,kCGLineCapButt);



//渲染第二条线的图形到view上

//绘制一条空心的线

CGContextStrokePath(ctx);

}





@end





效果:









二、画三角形



代码:





复制代码代码如下:





//

//YYrectview.m

//02-画三角形

//

//Createdby孔医己on14-6-10.

//Copyright(c)2014年itcast.Allrightsreserved.

//



#import"YYrectview.h"



@implementationYYrectview





-(void)drawRect:(CGRect)rect

{

//1.获得图形上下文

CGContextRefctx=UIGraphicsGetCurrentContext();



//2.绘制三角形

//设置起点

CGContextMoveToPoint(ctx,20,100);

//设置第二个点

CGContextAddLineToPoint(ctx,40,300);

//设置第三个点

CGContextAddLineToPoint(ctx,200,200);

//设置终点

//CGContextAddLineToPoint(ctx,20,100);

//关闭起点和终点

CGContextClosePath(ctx);



//3.渲染图形到layer上

CGContextStrokePath(ctx);



}





@end





效果:









提示:关闭起点和终点CGContextClosePath(ctx);



三、画四边形



代码:





复制代码代码如下:





//

//YYrect.m

//03-画四边形

//

//Createdby孔医己on14-6-10.

//Copyright(c)2014年itcast.Allrightsreserved.

//



#import"YYrect.h"



@implementationYYrect





-(void)drawRect:(CGRect)rect

{



//1.获取图形上下文

CGContextRefctx=UIGraphicsGetCurrentContext();

//2.画四边形

CGContextAddRect(ctx,CGRectMake(20,20,150,100));



//如果要设置绘图的状态必须在渲染之前

//CGContextSetRGBStrokeColor(ctx,1.0,0,0,1.0);

//绘制什么类型的图形(空心或者实心).就要通过什么类型的方法设置状态

//CGContextSetRGBFillColor(ctx,1.0,0,0,1.0);



//调用OC的方法设置绘图的颜色

//[[UIColorpurpleColor]setFill];

//[[UIColorblueColor]setStroke];

//调用OC的方法设置绘图颜色(同时设置了实心和空心)

//[[UIColorgreenColor]set];

[[UIColorcolorWithRed:1.0green:0blue:0alpha:1.0]set];





//3.渲染图形到layer上

//空心的

CGContextStrokePath(ctx);

//实心的

//CGContextFillPath(ctx);



}





@end





提示:如果要设置绘图的状态必须在渲染之前。



效果(实心和空心):









四、画圆



代码1:





复制代码代码如下:





-(void)drawRect:(CGRect)rect

{



//1.获取上下文

CGContextRefctx=UIGraphicsGetCurrentContext();

//画圆

CGContextAddArc(ctx,100,100,50,0,2M_PI,0);



//3.渲染(注意,画线只能通过空心来画)

//CGContextFillPath(ctx);

CGContextStrokePath(ctx);



}





效果:









代码2:





复制代码代码如下:





//画圆

//1.获取上下文

CGContextRefctx=UIGraphicsGetCurrentContext();

//2.画圆

CGContextAddEllipseInRect(ctx,CGRectMake(50,100,50,50));



[[UIColorgreenColor]set];



//3.渲染

//CGContextStrokePath(ctx);

CGContextFillPath(ctx);





效果:









代码3:





复制代码代码如下:





//画椭圆

//1.获取上下文

CGContextRefctx=UIGraphicsGetCurrentContext();

//2.画圆

CGContextAddEllipseInRect(ctx,CGRectMake(50,100,100,230));



[[UIColorpurpleColor]set];



//3.渲染

//CGContextStrokePath(ctx);

CGContextFillPath(ctx);





效果:









五、画圆弧



代码1:





复制代码代码如下:





//画圆弧

//1.获取上下文

CGContextRefctx=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);





效果:









代码2:





复制代码代码如下:





//1.获取上下文

CGContextRefctx=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);

[[UIColorbrownColor]set];





//3.渲染(注意,画线只能通过空心来画)

CGContextFillPath(ctx);

//CGContextStrokePath(ctx);





效果:









六、画文字

代码:





复制代码代码如下:





//

//YYtextview.m

//04-写文字

//

//Createdby孔医己on14-6-10.

//Copyright(c)2014年itcast.Allrightsreserved.

//



#import"YYtextview.h"



@implementationYYtextview





-(void)drawRect:(CGRect)rect

{



//画文字

NSStringstr=@"的额搜风搜分手了粉色发俄双方说法offFF瓦房你F回复F入会费WFH;飞;FN返回WFH;哦发货;F回复;FHISFHSIFH我皮肤好APIFRHi分红AWFHIOF威锋网i";



//1.获取上下文

//CGContextRefctx=UIGraphicsGetCurrentContext();

//2.绘图

//不推荐使用C语言的方法绘制文字,因为quraz2d中的坐标系和UIkit中的坐标系不一致,绘制出来的文字是颠倒的,而且通过C语言的方法绘制文字相当麻烦

//CGContextSelectFont(<#CGContexwww.hunanwang.nettRefc#>,<#constcharname#>,<#CGFloatsize#>,<#CGTextEncodingtextEncoding#>)

//CGContextShowText(ctx,<#constcharstring#>,<#size_tlength#>)



//绘制矩形

//1.获取上下文

CGContextRefctx=UIGraphicsGetCurrentContext();

//2.绘图

CGContextAddRect(ctx,CGRectMake(50,50,100,100));

//3.渲染

CGContextStrokePath(ctx);





//NSMutableDictionarymd=[NSMutableDictionarydictionary];

////设置文字颜色

//md[NSForegroundColorAttributeName]=[UIColorredColor];

////设置文字背景颜色

//md[NSBackgroundColorAttributeName]=[UIColorgreenColor];

////设置文字大小

//md[NSFontAttributeName]=[UIFontsystemFontOfSize:20];



//将文字绘制到指点的位置

//[strdrawAtPoint:CGPointMake(10,10)withAttributes:md];



//将文字绘制到指定的范围内,如果一行装不下会自动换行,当文字超出范围后就不显示

[strdrawInRect:CGRectMake(50,50,100,100)withAttributes:nil];

}





@end





效果:









图片



代码1:





复制代码代码如下:





//

//YYimage.m

//04-写文字

//

//Createdby孔医己on14-6-10.

//Copyright(c)2014年itcast.Allrightswww.visa158.comreserved.

//



#import"YYimage.h"



@implementationYYimage





-(void)drawRect:(CGRect)rect

{



//1.加载图片到内存中

UIImageimage=[UIImageimageNamed:@"me"];





//利用drawAsPatternInRec方法绘制图片到layer,是通过平铺原有图片

[imagedrawAsPatternInRect:CGRectMake(0,0,320,480)];

}





@end





效果(平铺):









代码2:





复制代码代码如下:





#import"YYimage.h"



@implementationYYimage





-(void)drawRect:(CGRect)rect

{



//1.加载图片到内存中

UIImageimage=[UIImageimageNamed:@"me"];





//利用OC方法将图片绘制到layer上



//利用drawInRect方法绘制图片到layer,是通过拉伸原有图片

[imagedrawInRect:CGRectMake(0,0,200,200)];



//利用drawAsPatternInRec方法绘制图片到layer,是通过平铺原有图片

//[imagedrawAsPatternInRect:CGRectMake(0,0,320,480)];

}





@end





效果(拉伸图片):









代码3:





复制代码代码如下:





//

//YYimage.m

//04-写文字

//

//Createdby孔医己on14-6-10.

//Copyright(c)2014年itcast.Allrightsreserved.

//



#import"YYimage.h"



@implementationYYimage





-(void)drawRect:(CGRect)rect

{



//1.加载图片到内存中

UIImageimage=[UIImageimageNamed:@"me"];





//利用OC方法将图片绘制到layer上



//将图片绘制到指定的位置

[imagedrawAtPoint:CGPointMake(100,100)];

}





效果(把图片绘制到一个固定的位置):





















献花(0)
+1
(本文系白狐一梦首藏)