首先了解一下cgcontextref:
an opaque type that represents a quartz 2d drawing environment.
graphics context是圖形上下文,可以将其了解為一塊畫布,我們可以在上面進行繪畫操作,繪制完成後,将畫布放到我們的view中顯示即可,view看作是一個畫框.
自己學習時實作的demo,希望對大家有幫助,具體的實作看代碼,并有完美的注釋解釋,還有一些對我幫助的博文供大家參考。都在代碼裡面。
看一下demo效果圖先:
自定義customview類,customview.h:
#import <uikit/uikit.h>
#import <quartzcore/quartzcore.h>
#define pi 3.14159265358979323846
@interface customview : uiview
@end
實作類customview.m:
#import "customview.h"
@implementation customview
- (id)initwithframe:(cgrect)frame
{
self = [super initwithframe:frame];
if (self) {
}
return self;
}
// 覆寫drawrect方法,你可以在此自定義繪畫和動畫
- (void)drawrect:(cgrect)rect
//an opaque type that represents a quartz 2d drawing environment.
//一個不透明類型的quartz 2d繪畫環境,相當于一個畫布,你可以在上面任意繪畫
cgcontextref context = uigraphicsgetcurrentcontext();
/*寫文字*/
cgcontextsetrgbfillcolor (context, 1, 0, 0, 1.0);//設定填充顔色
uifont *font = [uifont boldsystemfontofsize:15.0];//設定
[@"畫圓:" drawinrect:cgrectmake(10, 20, 80, 20) withfont:font];
[@"畫線及孤線:" drawinrect:cgrectmake(10, 80, 100, 20) withfont:font];
[@"畫矩形:" drawinrect:cgrectmake(10, 120, 80, 20) withfont:font];
[@"畫扇形和橢圓:" drawinrect:cgrectmake(10, 160, 110, 20) withfont:font];
[@"畫三角形:" drawinrect:cgrectmake(10, 220, 80, 20) withfont:font];
[@"畫圓角矩形:" drawinrect:cgrectmake(10, 260, 100, 20) withfont:font];
[@"畫貝塞爾曲線:" drawinrect:cgrectmake(10, 300, 100, 20) withfont:font];
[@"圖檔:" drawinrect:cgrectmake(10, 340, 80, 20) withfont:font];
/*畫圓*/
//邊框圓
cgcontextsetrgbstrokecolor(context,1,1,1,1.0);//畫筆線的顔色
cgcontextsetlinewidth(context, 1.0);//線的寬度
//void cgcontextaddarc(cgcontextref c,cgfloat x, cgfloat y,cgfloat radius,cgfloat startangle,cgfloat endangle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y為圓點坐标,radius半徑,startangle為開始的弧度,endangle為 結束的弧度,clockwise 0為順時針,1為逆時針。
cgcontextaddarc(context, 100, 20, 15, 0, 2*pi, 0); //添加一個圓
cgcontextdrawpath(context, kcgpathstroke); //繪制路徑
//填充圓,無邊框
cgcontextaddarc(context, 150, 30, 30, 0, 2*pi, 0); //添加一個圓
cgcontextdrawpath(context, kcgpathfill);//繪制填充
//畫大圓并填充顔
uicolor*acolor = [uicolor colorwithred:1 green:0.0 blue:0 alpha:1];
cgcontextsetfillcolorwithcolor(context, acolor.cgcolor);//填充顔色
cgcontextsetlinewidth(context, 3.0);//線的寬度
cgcontextaddarc(context, 250, 40, 40, 0, 2*pi, 0); //添加一個圓
//kcgpathfill填充非零繞數規則,kcgpatheofill表示用奇偶規則,kcgpathstroke路徑,kcgpathfillstroke路徑填充,kcgpatheofillstroke表示描線,不是填充
cgcontextdrawpath(context, kcgpathfillstroke); //繪制路徑加填充
/*畫線及孤線*/
//畫線
cgpoint apoints[2];//坐标點
apoints[0] =cgpointmake(100, 80);//坐标1
apoints[1] =cgpointmake(130, 80);//坐标2
//cgcontextaddlines(cgcontextref c, const cgpoint points[],size_t count)
//points[]坐标數組,和count大小
cgcontextaddlines(context, apoints, 2);//添加線
cgcontextdrawpath(context, kcgpathstroke); //根據坐标繪制路徑
//畫笑臉弧線
//左
cgcontextsetrgbstrokecolor(context, 0, 0, 1, 1);//改變畫筆顔色
cgcontextmovetopoint(context, 140, 80);//開始坐标p1
//cgcontextaddarctopoint(cgcontextref c, cgfloat x1, cgfloat y1,cgfloat x2, cgfloat y2, cgfloat radius)
//x1,y1跟p1形成一條線的坐标p2,x2,y2結束坐标跟p3形成一條線的p3,radius半徑,注意, 需要算好半徑的長度,
cgcontextaddarctopoint(context, 148, 68, 156, 80, 10);
cgcontextstrokepath(context);//繪畫路徑
//右
cgcontextmovetopoint(context, 160, 80);//開始坐标p1
cgcontextaddarctopoint(context, 168, 68, 176, 80, 10);
cgcontextmovetopoint(context, 150, 90);//開始坐标p1
cgcontextaddarctopoint(context, 158, 102, 166, 90, 10);
//注,如果還是沒弄明白怎麼回事,請參考:http://donbe.blog.163.com/blog/static/138048021201052093633776/
/*畫矩形*/
cgcontextstrokerect(context,cgrectmake(100, 120, 10, 10));//畫方框
cgcontextfillrect(context,cgrectmake(120, 120, 10, 10));//填充框
//矩形,并填棄顔色
cgcontextsetlinewidth(context, 2.0);//線的寬度
acolor = [uicolor bluecolor];//blue藍色
acolor = [uicolor yellowcolor];
cgcontextsetstrokecolorwithcolor(context, acolor.cgcolor);//線框顔色
cgcontextaddrect(context,cgrectmake(140, 120, 60, 30));//畫方框
cgcontextdrawpath(context, kcgpathfillstroke);//繪畫路徑
//矩形,并填棄漸變顔色
//關于顔色參考http://blog.sina.com.cn/s/blog_6ec3c9ce01015v3c.html
//http://blog.csdn.net/reylen/article/details/8622932
//第一種填充方式,第一種方式必須導入類庫quartcore并#import <quartzcore/quartzcore.h>,這個就不屬于在context上畫,而是将層插入到view層上面。那麼這裡就設計到quartz core 圖層程式設計了。
cagradientlayer *gradient1 = [cagradientlayer layer];
gradient1.frame = cgrectmake(240, 120, 60, 30);
gradient1.colors = [nsarray arraywithobjects:(id)[uicolor whitecolor].cgcolor,
(id)[uicolor graycolor].cgcolor,
(id)[uicolor blackcolor].cgcolor,
(id)[uicolor yellowcolor].cgcolor,
(id)[uicolor bluecolor].cgcolor,
(id)[uicolor redcolor].cgcolor,
(id)[uicolor greencolor].cgcolor,
(id)[uicolor orangecolor].cgcolor,
(id)[uicolor browncolor].cgcolor,nil];
[self.layer insertsublayer:gradient1 atindex:0];
//第二種填充方式
cgcolorspaceref rgb = cgcolorspacecreatedevicergb();
cgfloat colors[] =
{
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,
};
cggradientref gradient = 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
//再寫一個看看效果
cgcontextmovetopoint(context, 260, 90);
cgcontextaddlinetopoint(context, 280, 90);
cgcontextaddlinetopoint(context, 280, 100);
cgcontextaddlinetopoint(context, 260, 100);
cgcontextclip(context);//裁剪路徑
//說白了,開始坐标和結束坐标是控制漸變的方向和形狀
(260, 90) ,cgpointmake(260, 100),
//下面再看一個顔色漸變的圓
cgcontextdrawradialgradient(context, gradient, cgpointmake(300, 100), 0.0, cgpointmake(300, 100), 10, kcggradientdrawsbeforestartlocation);
/*畫扇形和橢圓*/
//畫扇形,也就畫圓,隻不過是設定角度的大小,形成一個扇形
acolor = [uicolor colorwithred:0 green:1 blue:1 alpha:1];
//以10為半徑圍繞圓心畫指定角度扇形
cgcontextmovetopoint(context, 160, 180);
cgcontextaddarc(context, 160, 180, 30, -60 * pi / 180, -120 * pi / 180, 1);
cgcontextclosepath(context);
cgcontextdrawpath(context, kcgpathfillstroke); //繪制路徑
//畫橢圓
cgcontextaddellipseinrect(context, cgrectmake(160, 180, 20, 8)); //橢圓
cgcontextdrawpath(context, kcgpathfillstroke);
/*畫三角形*/
//隻要三個點就行跟畫一條線方式一樣,把三點連接配接起來
cgpoint spoints[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); //根據坐标繪制路徑
/*畫圓角矩形*/
float fw = 180;
float fh = 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); // 右上角
/*畫貝塞爾曲線*/
//二次曲線
cgcontextmovetopoint(context, 120, 300);//設定path的起點
cgcontextaddquadcurvetopoint(context,190, 310, 120, 390);//設定貝塞爾曲線的控制點坐标和終點坐标
cgcontextstrokepath(context);
//三次曲線函數
cgcontextmovetopoint(context, 200, 300);//設定path的起點
cgcontextaddcurvetopoint(context,250, 280, 250, 400, 280, 300);//設定貝塞爾曲線的控制點坐标和控制點坐标終點坐标
/*圖檔*/
uiimage *image = [uiimage imagenamed:@"apple.jpg"];
[image drawinrect:cgrectmake(60, 340, 20, 20)];//在坐标中畫出圖檔
// [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);//平鋪圖
用法:
customview *customview = [[customview alloc]initwithframe:cgrectmake(0, 0, 320, self.view.frame.size.height)];
[self.view addsubview:customview];
ok,完成。
當年寫andriod的時候寫的一遍部落格。
請大家尊重一下我的勞動成功,轉載請注明原創位址!
<a target="_blank" href="http://blog.csdn.net/rhljiayou/article/details/9919713">http://blog.csdn.net/rhljiayou/article/details/9919713</a>