/*
?作用:專門用來繪圖
?什么時(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);
}