Quartz 2D 是 iOS 早期常見的繪圖方式,适合作靜態圖形、路徑、矩形、圓形與簡單動畫的底層繪製。若想自訂畫面,通常會在 UIView 子類別中覆寫 drawRect:。
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextAddRect(context, CGRectMake(50, 50, 100, 100)); CGContextDrawPath(context, kCGPathEOFill); CGContextClosePath(context);
Quartz 2D 可做平移、旋轉與縮放。轉換前建議先儲存狀態,完成後再恢復,避免影響後續繪圖。
CGContextBeginPath CGContextMoveToPoint CGContextClosePath CGContextAddLineToPoint CGContextAddRect CGContextAddEllipseInRect CGContextAddArc CGContextAddCurveToPoint CGContextDrawPath CGContextFillRect CGContextStrokeRect CGContextSetLineWidth CGContextSetLineDash
這些 API 能組合出多數基礎介面元素。實際開發時,建議先釐清尺寸與座標,再進行繪製,較容易維持畫面穩定。