線段代碼展示:
代碼:
- (void)drawRect:(CGRect)rect
{
CGContextRef line = UIGraphicsGetCurrentContext(); // 不需要* 如贷,獲得圖形上下文
CGContextMoveToPoint(line, 50, 50); // 設(shè)置線段起點
CGContextAddLineToPoint(line, 250, 50); // 設(shè)置線段重點
CGContextSetRGBStrokeColor(line, 0, 1.0, 0, 1.0); // 設(shè)置險段的顏色
CGContextSetLineWidth(line, 10); // 設(shè)置線斷的寬度
CGContextSetLineJoin(line, kCGLineJoinRound); // 設(shè)置線斷起點和終點的樣式都為圓角
CGContextSetLineCap(line, kCGLineCapRound); // 設(shè)置險段的轉(zhuǎn)角樣式為圓角
CGContextStrokePath(line); // 渲染,繪制出一條空心的線斷
CGContextRef lineOne = UIGraphicsGetCurrentContext(); // 設(shè)置第二條線
CGContextMoveToPoint(lineOne, 50, 150); // 設(shè)置線段起點
CGContextAddLineToPoint(lineOne, 200, 20); // 設(shè)置線段終點
CGContextSetRGBStrokeColor(lineOne, 1.0, 0, 0, 1.0); // 設(shè)置線斷顏色
CGContextSetLineWidth(lineOne, 10); // 設(shè)置線段寬度
CGContextStrokePath(lineOne); // 渲染,繪制出一條空心的線斷
}
@end
三角形代碼展示:
代碼:
- (void)drawRect:(CGRect)rect
{
CGContextRef triangle = UIGraphicsGetCurrentContext(); // 獲得圖形上下文
CGContextMoveToPoint(triangle, 150, 40); // 設(shè)置起點
CGContextAddLineToPoint(triangle, 60, 200); // 設(shè)置第二個點
CGContextAddLineToPoint(triangle,240, 200); // 設(shè)置第三個點
CGContextClosePath(triangle); // 關(guān)閉起點和終點
CGContextStrokePath(triangle); // 渲染杠袱,繪制出三角形
}
@end
實心四邊形尚猿、空心四邊形,代碼展示:
代碼:
- (void)drawRect:(CGRect)rect
{
CGContextRef quadrilateral = UIGraphicsGetCurrentContext(); // 獲得圖形上下文
CGContextAddRect(quadrilateral, CGRectMake(45, 45, 200, 200)); // 設(shè)置起始坐標(biāo)楣富,以及長和寬
CGContextSetRGBFillColor(quadrilateral, 1.0, 1.0, 0, 1); // 設(shè)置實心顏色
//CGContextSetRGBStrokeColor(quadrilateral, 1.0, 1.0, 0, 1.0); // 設(shè)置空心顏色
//CGContextStrokePath(quadrilateral); // 渲染空心凿掂,繪制四邊形
CGContextFillPath(quadrilateral); // 渲染實心,繪制出四邊形
}
@end
實心圓形纹蝴、空心圓形庄萎、橢圓,代碼展示:
空心圓形代碼:
- (void)drawRect:(CGRect)rect
{
CGContextRef round = UIGraphicsGetCurrentContext(); // 獲取上下文
CGContextAddEllipseInRect(round, CGRectMake(50, 50, 100, 100)); // 畫圓
CGContextSetRGBStrokeColor(round, 0, 0, 0, 1);// 設(shè)置顏色
CGContextStrokePath(round); // 渲染塘安,將空心圓形畫出
}
@end
橢圓代碼:
- (void)drawRect:(CGRect)rect
{
CGContextRef circular = UIGraphicsGetCurrentContext(); // 獲取上下文
CGContextAddEllipseInRect(circular, CGRectMake(100, 100, 100, 100)); // 畫圓(100,100 變成不等大的就是橢圓糠涛,這里不再貼重復(fù)代碼)
CGContextSetRGBFillColor(circular, 1.0, 0, 1.0, 1);// 設(shè)置顏色
CGContextFillPath(circular); // 渲染,將實心圓形畫出
}
@end
實心圓弧兼犯、空心圓弧忍捡、圓環(huán),代碼展示:
實心圓磺星:
- (void)drawRect:(CGRect)rect
{
CGContextRef arc = UIGraphicsGetCurrentContext(); // 獲取上下文
CGContextMoveToPoint(arc, 150, 150); // 畫線
CGContextAddLineToPoint(arc, 150, 150); // 畫線
CGContextAddArc(arc, 150, 150, 100, M_PI_2, M_PI, 0); // (150,150)圓心 (100)半徑 (M_PI_2) 弧度開始的大小 (M_PI) 弧度結(jié)束的大小 (0,1)順時針, 逆時針
CGContextClosePath(arc); // 關(guān)閉路徑
[[UIColor purpleColor]set]; // 設(shè)置顏色
CGContextFillPath(arc); // 3.渲染( Fill 改成 Stroke 變成空心砸脊,不重復(fù)貼代碼)
}
@end
環(huán)形代碼:
- (void)drawRect:(CGRect)rect
{
CGContextRef annular = UIGraphicsGetCurrentContext();
CGContextAddArc(annular, 100, 100, 50, 1, 20, 0);
CGContextSetLineWidth(annular, 10);
[[UIColor greenColor]set];
CGContextStrokePath(annular);
}
@end