ios基本線條繪制

/*

?作用:專門用來繪圖

?什么時(shí)候調(diào)用:系統(tǒng)自動(dòng)調(diào)用,當(dāng)View顯示的時(shí)候調(diào)用

?param rect:當(dāng)前view的bounds

?*/

-(void)drawRect:(CGRect)rect{

? ? //1、在drawRect方法當(dāng)中系統(tǒng)已經(jīng)幫你創(chuàng)建一個(gè)跟view相關(guān)聯(lián)的上下文(Layer)


? ? //[self drawLine];


? ? //畫曲線


? ? //1、獲取上下文

? ? CGContextRef ctx = UIGraphicsGetCurrentContext();

? ? //2、繪制路徑

? ? UIBezierPath *path = [UIBezierPath bezierPath];

? ? //畫曲線

? ? [pathmoveToPoint:CGPointMake(50, 200)];

? ? //添加一根曲線到某一點(diǎn)

? ? [pathaddQuadCurveToPoint:CGPointMake(250, 200) controlPoint:CGPointMake(50, 50)];

? ? //3、把繪制的內(nèi)容保存到上下文當(dāng)中

? ? CGContextAddPath(ctx, path.CGPath);

? ? //4、把上下文內(nèi)容顯示到view上

? ? CGContextStrokePath(ctx);

}

//畫直線

-(void)drawLine{

? ? //1、獲取上下文(獲取、創(chuàng)建上下文都以UIGraphic開頭)

? ? CGContextRef ctx = UIGraphicsGetCurrentContext();

? ? //2、繪制路徑

? ? UIBezierPath *path = [UIBezierPath bezierPath];

? ? //2.1:設(shè)置起點(diǎn)

? ? CGPointbeginPoint =CGPointMake(50,280);

? ? CGPointendPoint =CGPointMake(250,50);

? ? [pathmoveToPoint:beginPoint];

? ? //2.2:添加一根線到終點(diǎn)

? ? [pathaddLineToPoint:endPoint];


? ? //畫第二條線

? ? //[path moveToPoint:CGPointMake(100, 200)];

? ? [pathaddLineToPoint:CGPointMake(250, 150)];


? ? //上下文狀態(tài)

? ? //設(shè)置線寬

? ? CGContextSetLineWidth(ctx, 10);

? ? //設(shè)置線的連接樣式

? ? CGContextSetLineJoin(ctx, kCGLineJoinRound);

? ? //設(shè)置線的頂角樣式

? ? CGContextSetLineCap(ctx, kCGLineCapRound);

? ? //設(shè)置顏色

? ? [[UIColor redColor] set];



? ? //3、把繪制的內(nèi)容保存到上下文當(dāng)中

? ? //CGPathRef:CoreGraphics框架。UIBezierPath:UIKit框架

? ? CGContextAddPath(ctx, path.CGPath);


? ? //4、把上下文內(nèi)容顯示到view上(渲染到View的layer)(stroke,fill)

? ? CGContextStrokePath(ctx);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容