iOS開發(fā)CGContextRef畫圖使用總結(jié)
本文實(shí)例為大家匯總了iOS開發(fā)CGContextRef畫圖使用,供大家參考,具體內(nèi)容如下
1.創(chuàng)建畫布
CGContextRef ctx = UIGraphicsGetCurrentContext();
2.設(shè)置屬性
//旋轉(zhuǎn),注意:設(shè)置操作必須要在添加圖形之前,如果設(shè)置在添加圖形之后的話,此時(shí)它已經(jīng)畫完了,無效 //旋轉(zhuǎn)的時(shí)候,是整個(gè)layer都旋轉(zhuǎn)了 //旋轉(zhuǎn)45度 CGContextRotateCTM(ctx, M_PI_4); //縮放:x方向縮放0.5倍,y方向縮放1.5倍 CGContextScaleCTM(ctx, 0.5, 1.5); //平移:x方向移動(dòng)50,y方向移動(dòng)100 CGContextTranslateCTM(ctx, 50, 100); CGContextSetLineJoin(ctx, kCGLineJoinRound); //線條寬度 CGContextSetLineWidth(ctx, 1.0); //起點(diǎn)和終點(diǎn)圓角 CGContextSetLineCap(ctx, kCGLineCapRound); //轉(zhuǎn)角圓角 CGContextSetLineJoin(ctx, kCGLineJoinRound); //透明度 CGContextSetAlpha(ctx, 0.5)
3.畫直線
//起點(diǎn) CGContextMoveToPoint(ctx, 10.0, 100.0); //終點(diǎn) CGContextAddLineToPoint(ctx, self.frame.size.width-20.0, 100.0); //顏色 兩種設(shè)置顏色的方式都可以 //CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0); [[UIColor redColor] set]; //渲染,直線只能繪制空心的,不能調(diào)用CGContextFillPath(ctx) // 或者使用這個(gè)方法:CGContextDrawPath(ctx, kCGPathStroke); CGContextStrokePath(ctx);
或者使用下面方法畫直線
CGPoint point[2];//坐標(biāo)點(diǎn) ? point[0] = CGPointMake(10.0, 100.0);//起點(diǎn) ? point[1] = CGPointMake(self.frame.size.width-20.0, 100.0);//終點(diǎn) ?? //points[]坐標(biāo)數(shù)組,和count大小 ? CGContextAddLines(context, aPoints, 2);//添加線 ? CGContextDrawPath(context, kCGPathStroke);
4.畫虛線
//設(shè)置虛線顏色
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
//設(shè)置虛線繪制起點(diǎn)
CGContextMoveToPoint(ctx, 10.0, 50.0);
//設(shè)置虛線繪制終點(diǎn)
CGContextAddLineToPoint(ctx, self.frame.size.width-20.0, 50.0);
//設(shè)置虛線排列的寬度間隔:下面的arr中的數(shù)字表示先繪制3個(gè)點(diǎn)再繪制1個(gè)點(diǎn)
CGFloat arr[] = {3, 2};
//下面最后一個(gè)參數(shù)“2”代表排列的個(gè)數(shù)。
CGContextSetLineDash(ctx, 0, arr, 2);
CGContextDrawPath(ctx, kCGPathStroke);5.畫三角形
//起點(diǎn) CGContextMoveToPoint(ctx, self.center.x, 200.0); //拐點(diǎn)1 CGContextAddLineToPoint(ctx, self.center.x-50.0, 250.0); //終點(diǎn) CGContextAddLineToPoint(ctx, self.center.x+50.0, 250.0); //顏色 兩種設(shè)置顏色的方式都可以 //CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0); [[UIColor redColor] set]; //合并三角形 CGContextClosePath(ctx); CGContextFillPath(ctx);
6.畫矩形
CGRect rectangle = CGRectMake(10.0, 300.0, self.frame.size.width-20.0, 60.0); CGContextAddRect(ctx, rectangle); CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); CGContextFillPath(ctx);
7.畫圓
/** c ? ? ? ? ? 當(dāng)前圖形 x ? ? ? ? ? 圓心坐標(biāo)x y ? ? ? ? ? 圓心坐標(biāo)y radius ? ? ?半徑 startAngle ?弧的起點(diǎn)與正X軸的夾角 endAngle ? ?弧的終點(diǎn)與正X軸的夾角 clockwise ? 指定0創(chuàng)建一個(gè)順時(shí)針的圓弧,或是指定1創(chuàng)建一個(gè)逆時(shí)針圓弧 */ CGContextAddArc(ctx, self.center.x, 100.0, 75.0, 0.0, M_PI+0.5, 0); CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); CGContextFillPath(ctx);
8.畫橢圓
CGContextAddEllipseInRect(ctx, CGRectMake(x, y, 100.0, 60.0)); CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); CGContextFillPath(ctx);
9.畫扇形
CGContextMoveToPoint(ctx, x, y); CGContextAddArc(ctx, x, y, 75.0, 0.0, M_PI+0.5, 0); CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor); CGContextDrawPath(ctx, kCGPathFillStroke);
10.畫二次貝塞爾曲線
CGContextMoveToPoint(context, 120, 300);//設(shè)置Path的起點(diǎn) ? CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);//設(shè)置貝塞爾曲線的控制點(diǎn)坐標(biāo)和終點(diǎn)坐標(biāo) ? CGContextStrokePath(context);
11.畫三次貝塞爾曲線
CGContextMoveToPoint(context, 200, 300);//設(shè)置Path的起點(diǎn) ? CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);//設(shè)置貝塞爾曲線的控制點(diǎn)坐標(biāo)和控制點(diǎn)坐標(biāo)終點(diǎn)坐標(biāo) ? CGContextStrokePath(context);
12.畫文字
// 設(shè)置文字的屬性 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; dict[NSFontAttributeName] = [UIFont systemFontOfSize:14]; [@"I Love iOS" drawInRect:rect withAttributes:dict];
13.畫圖片
UIImage *image = [UIImage imageNamed:@"apple.jpg"]; ? [image drawInRect:CGRectMake(60, 340, 20, 20)];//在坐標(biāo)中畫出圖片 ? //[image drawAtPoint:CGPointMake(100, 340)];//保持圖片大小在point點(diǎn)開始畫圖片,可以把注釋去掉看看 ? CGContextDrawImage(context, CGRectMake(100, 340, 20, 20), image.CGImage);//使用這個(gè)使圖片上下顛倒了? ? ?? //CGContextDrawTiledImage(context, CGRectMake(0, 0, 20, 20), image.CGImage);//平鋪圖?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ios實(shí)現(xiàn)簡(jiǎn)易隊(duì)列
這篇文章主要為大家詳細(xì)介紹了ios實(shí)現(xiàn)簡(jiǎn)易隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
舉例講解設(shè)計(jì)模式中的原型模式在iOS應(yīng)用開發(fā)中的作用
這篇文章主要介紹了設(shè)計(jì)模式中的原型模式在iOS應(yīng)用開發(fā)中的作用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解
這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
iOS創(chuàng)建對(duì)象的不同姿勢(shì)詳解
這篇文章主要介紹了iOS創(chuàng)建對(duì)象的不同姿勢(shì),文中介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,有需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-02-02
iOS開發(fā)之運(yùn)動(dòng)事件和遠(yuǎn)程控制
在iOS中事件分為三類:觸摸事件:通過觸摸、手勢(shì)進(jìn)行觸發(fā)(例如手指點(diǎn)擊、縮放),運(yùn)動(dòng)事件:通過加速器進(jìn)行觸發(fā)(例如手機(jī)晃動(dòng)),遠(yuǎn)程控制事件:通過其他遠(yuǎn)程設(shè)備觸發(fā)(例如耳機(jī)控制按鈕)今天我們來詳細(xì)探討下運(yùn)動(dòng)事件和遠(yuǎn)程控制2016-04-04
iOS系統(tǒng)和微信中不支持audio自動(dòng)播放問題的解決方法
最近在微信端開發(fā)H5的時(shí)候,audio標(biāo)簽在蘋果機(jī)上無法進(jìn)行自動(dòng)播放,查找相關(guān)資料終于解決了,所以下面這篇文章主要給大家介紹了關(guān)于iOS系統(tǒng)和微信中不支持audio自動(dòng)播放問題的解決方法,需要的朋友可以參考下。2017-09-09

