Graphics Context(圖形上下文)可以想成一塊畫布,我們在上面落筆作畫,畫完再把這塊畫布掛到 view 上呈現——而 view 本身就像一個畫框。CGContextRef 的能力很強,幾乎所有自訂繪製都繞不開它,把這些基礎筆法練熟,之後處理複雜 UI 會輕oler很多。
先寫一個繼承自 UIView 的 CustomView,繪圖邏輯全部放在 drawRect: 裡;接著在檢視控制器中把它加進來:
#import <QuartzCore/QuartzCore.h>
// 建立並加入畫布
CustomView *customView = [[CustomView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:customView];
- (void)drawRect:(CGRect)rect {
// 取得目前畫布
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 設定線條顏色
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
// 設定線寬
CGContextSetLineWidth(ctx, 0.25);
// 落筆寫字
[@"我是文字" drawInRect:CGRectMake(10, 10, 100, 30) withFont:font];
[super drawRect:rect];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
CGContextSetLineWidth(ctx, 0.25);
// 頂部橫線:先移動到起點,再連到終點,最後描邊
CGContextMoveToPoint(ctx, 0, 10);
CGContextAddLineToPoint(ctx, self.bounds.size.width, 10);
CGContextStrokePath(ctx);
[super drawRect:rect];
}
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); // 換成藍色畫筆 CGContextMoveToPoint(context, 140, 80); // 起始座標 p1 // CGContextAddArcToPoint(c, x1, y1, x2, y2, radius) // (x1,y1) 與 p1 連成一線,(x2,y2) 為結束座標, // radius 為圓角半徑,半徑長度需事先算好 CGContextAddArcToPoint(context, 148, 68, 156, 80, 10); CGContextStrokePath(context); // 描邊繪製路徑
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
CGContextSetLineWidth(ctx, 0.25);
// CGContextAddArc(c, x, y, radius, startAngle, endAngle, clockwise)
// 1 弧度 = 180°/π(≈57.3°);度 = 弧度 × 180°/π;360° = 2π 弧度
// x,y 為圓心,startAngle / endAngle 為起訖弧度,clockwise 為 0 順時針、1 逆時針
CGContextAddArc(ctx, 100, 20, 20, 0, 2 * M_PI, 0);
CGContextDrawPath(ctx, kCGPathStroke);
[super drawRect:rect];
}
順手複習一下:M_PI 就是 π,約等於 3.14159265358979,所以整圈是 2 * M_PI。
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 * M_PI, 0); // kCGPathFill:非零繞數規則填滿;kCGPathEOFill:奇偶規則; // kCGPathStroke:只描邊;kCGPathFillStroke:描邊加填滿; // kCGPathEOFillStroke:奇偶規則填滿加描邊 CGContextDrawPath(context, kCGPathFillStroke);
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);
CGContextSetLineWidth(ctx, 0.25);
CGContextAddRect(ctx, CGRectMake(2, 2, 30, 30));
CGContextStrokePath(ctx);
[super drawRect:rect];
}
扇形其實就是「只畫一段角度的圓」:先從圓心出發,再沿著指定角度掃出弧線,最後封閉路徑。
aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1]; CGContextSetFillColorWithColor(context, aColor.CGColor); // 以 (160,180) 為圓心、半徑 30,掃出指定角度的扇形 CGContextMoveToPoint(context, 160, 180); CGContextAddArc(context, 160, 180, 30, -60 * PI / 180, -120 * PI / 180, 1); CGContextClosePath(context); CGContextDrawPath(context, kCGPathFillStroke);
// 二次曲線:一個控制點 CGContextMoveToPoint(context, 120, 300); // 起點 CGContextAddQuadCurveToPoint(context, 190, 310, 120, 390); // 控制點 + 終點 CGContextStrokePath(context); // 三次曲線:兩個控制點 CGContextMoveToPoint(context, 200, 300); // 起點 CGContextAddCurveToPoint(context, 250, 280, 250, 400, 280, 300); // 控制點×2 + 終點 CGContextStrokePath(context);
把這些筆法串起來,自訂進度條、氣泡框、繪圖板都不成問題。實作時記得一個觀念:CGContext 的狀態(顏色、線寬、線帽)都會沿著繪製流程延續,需要切換時明確設定,才不會出現「顏色莫名其妙跑掉」的狀況。