iOS draw出各種圖形

- (void)drawRect:(CGRect)rect {

//寫字

[@"程序員"drawInRect:CGRectMake(20,380,100,50)withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:20],NSForegroundColorAttributeName:[UIColorredColor]}];

CGContextRefcontext =UIGraphicsGetCurrentContext();

//畫線

CGPointaPoints[3];//坐標點

aPoints[0] =CGPointMake(100,80);//坐標1

aPoints[1] =CGPointMake(130,80);//坐標2

aPoints[2] =CGPointMake(130,100);//坐標3

CGContextAddLines(context, aPoints,3);//添加線

CGContextDrawPath(context,kCGPathStroke);//根據坐標繪制路徑

//弧線(方法一)

/*

從(140,80)到(148,68)畫一條直線,從(148,68)到(156,80)畫一條直線,兩條線之間畫一條10度的曲線

*/

CGContextSetRGBStrokeColor(context,89/255.0,89/255.0,89/255.0,1);//改變畫筆顏色

CGContextMoveToPoint(context,140,80);//開始坐標p1

CGContextAddArcToPoint(context,148,68,156,80,10);

CGContextStrokePath(context);//繪畫路徑

//弧線(方法二)

/*

以(200,100)為圓心,30為半徑,逆時針畫60度弧線(1為順時針,0為逆時針)

*/

CGContextAddArc(context,200,100,30,0,M_PI/3,0);

CGContextStrokePath(context);//繪畫路徑

//簡單矩形

CGContextStrokeRect(context,CGRectMake(100,150,10,10));//畫方框

CGContextFillRect(context,CGRectMake(120,180,10,10));//填充框

//畫矩形(填充顏色)

/*

*/

UIColor*aColor = [UIColorcolorWithRed:1green:0.0blue:0alpha:1];

//矩形,并填充顏色

CGContextSetLineWidth(context,2.0);//線的寬度

aColor = [UIColorblueColor];//blue藍色

CGContextSetFillColorWithColor(context, aColor.CGColor);//填充顏色

aColor = [UIColoryellowColor];

CGContextSetStrokeColorWithColor(context, aColor.CGColor);//線框顏色

CGContextAddRect(context,CGRectMake(140,220,60,30));//畫方框

CGContextDrawPath(context,kCGPathFillStroke);//繪畫路徑

//矩形(填充漸變顏色)

//方式一:

CAGradientLayer*gradient1 = [CAGradientLayerlayer];

gradient1.frame=CGRectMake(240,300,60,30);

gradient1.colors= [NSArrayarrayWithObjects:(id)[UIColorwhiteColor].CGColor,

(id)[UIColorgrayColor].CGColor,

(id)[UIColorblackColor].CGColor,

(id)[UIColoryellowColor].CGColor,

(id)[UIColorblueColor].CGColor,

(id)[UIColorredColor].CGColor,

(id)[UIColorgreenColor].CGColor,

(id)[UIColororangeColor].CGColor,

(id)[UIColorbrownColor].CGColor,nil];

[self.layerinsertSublayer:gradient1atIndex:0];

//方式二:

CGColorSpaceRefrgb =CGColorSpaceCreateDeviceRGB();

CGFloatcolors[] =

{

1,1,1,1.00,

1,1,0,1.00,

1,0,0,1.00,

1,0,1,1.00,

0,1,1,1.00,

0,1,0,1.00,

0,0,1,1.00,

0,0,0,1.00,

};

CGGradientRefgradient =CGGradientCreateWithColorComponents

(rgb, colors,NULL,sizeof(colors)/(sizeof(colors[0])*4));//形成梯形,漸變的效果

CGColorSpaceRelease(rgb);

//畫線形成一個矩形

//CGContextSaveGState與CGContextRestoreGState的作用

/*

CGContextSaveGState函數的作用是將當前圖形狀態推入堆棧。之后,您對圖形狀態所做的修改會影響隨后的描畫操作,但不影響存儲在堆棧中的拷貝。在修改完成后,您可以通過CGContextRestoreGState函數把堆棧頂部的狀態彈出,返回到之前的圖形狀態。這種推入和彈出的方式是回到之前圖形狀態的快速方法,避免逐個撤消所有的狀態修改;這也是將某些狀態(比如裁剪路徑)恢復到原有設置的唯一方式。

*/

CGContextSaveGState(context);

CGContextMoveToPoint(context,220,90);

CGContextAddLineToPoint(context,240,90);

CGContextAddLineToPoint(context,240,110);

CGContextAddLineToPoint(context,220,110);

CGContextClip(context);//context裁剪路徑,后續操作的路徑

//CGContextDrawLinearGradient(CGContextRef context,CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,CGGradientDrawingOptions options)

//gradient漸變顏色,startPoint開始漸變的起始位置,endPoint結束坐標,options開始坐標之前or開始之后開始漸變

CGContextDrawLinearGradient(context, gradient,CGPointMake

(220,90) ,CGPointMake(240,110),

kCGGradientDrawsAfterEndLocation);

CGContextRestoreGState(context);//恢復到之前的context

//再寫一個看看效果

CGContextSaveGState(context);

CGContextMoveToPoint(context,260,90);

CGContextAddLineToPoint(context,280,90);

CGContextAddLineToPoint(context,280,100);

CGContextAddLineToPoint(context,260,100);

CGContextClip(context);//裁剪路徑

//說白了,開始坐標和結束坐標是控制漸變的方向和形狀

CGContextDrawLinearGradient(context, gradient,CGPointMake

(260,90) ,CGPointMake(260,100),

kCGGradientDrawsAfterEndLocation);

CGContextRestoreGState(context);//恢復到之前的context

//顏色漸變的圓

[@"顏色漸變的圓:"drawInRect:CGRectMake(20,380,150,50)withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:20],NSForegroundColorAttributeName:[UIColorredColor]}];

CGContextDrawRadialGradient(context, gradient,CGPointMake(200,400),0.0,CGPointMake(200,400),10,kCGGradientDrawsBeforeStartLocation);

//畫橢圓

CGContextAddEllipseInRect(context,CGRectMake(160,180,20,8));//橢圓

CGContextDrawPath(context,kCGPathFillStroke);

//畫三角形

//只要三個點就行跟畫一條線方式一樣,把三點連接起來

CGPointsPoints[3];//坐標點

sPoints[0] =CGPointMake(100,220);//坐標1

sPoints[1] =CGPointMake(130,220);//坐標2

sPoints[2] =CGPointMake(130,160);//坐標3

CGContextAddLines(context, sPoints,3);//添加線

CGContextClosePath(context);//封起來

CGContextDrawPath(context,kCGPathFillStroke);//根據坐標繪制路徑

//*畫圓角矩形*/

floatfw =180;

floatfh =280;

CGContextMoveToPoint(context, fw, fh-20);//開始坐標右邊開始

CGContextAddArcToPoint(context, fw, fh, fw-20, fh,10);//右下角角度

CGContextAddArcToPoint(context,120, fh,120, fh-20,10);//左下角角度

CGContextAddArcToPoint(context,120,250, fw-20,250,10);//左上角

CGContextAddArcToPoint(context, fw,250, fw, fh-20,10);//右上角

CGContextClosePath(context);

CGContextDrawPath(context,kCGPathFillStroke);//根據坐標繪制路徑

/*畫貝塞爾曲線*/

//二次曲線

CGContextMoveToPoint(context,10,500);//設置Path的起點

CGContextAddQuadCurveToPoint(context,50,480,60,460);//設置貝塞爾曲線的控制點坐標和終點坐標

CGContextStrokePath(context);

//三次曲線函數

CGContextMoveToPoint(context,40,600);//設置Path的起點

CGContextAddCurveToPoint(context,50,620,80,650,100,700);//設置貝塞爾曲線的控制點坐標和控制點坐標終點坐標

CGContextStrokePath(context);

/*圖片*/

UIImage*image = [UIImageimageNamed:@"qianggou dianji-1"];

[imagedrawInRect:CGRectMake(60,600,40,40)];//在坐標中畫出圖片

//[image drawAtPoint:CGPointMake(100, 340)];//保持圖片大小在point點開始畫圖片,可以把注釋去掉看看

CGContextDrawImage(context,CGRectMake(100,340,20,20), image.CGImage);

//使用這個使圖片上下顛倒了,參考http://blog.csdn.net/koupoo/article/details/8670024

//CGContextDrawTiledImage(context, CGRectMake(0, 0, 20, 20), image.CGImage);//平鋪圖

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容