您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

Quartz 繪圖筆記:CGPathAddArc 與 CGPathAddArcToPoint 的用法與座標陷阱

編輯:iOS 開發綜合

用 Core Graphics 畫圓弧時,CGPathAddArc 是最直覺的選擇,但它有個讓很多人栽過跟頭的行為:順時針參數在 iOS 上的實際效果與文件相反。本文把兩個畫弧函數的用法與這個座標陷阱一起講清楚。

CGPathAddArc:圓心 + 半徑 + 兩段弧度

函式簽名:

void CGPathAddArc(CGMutablePathRef path,
                  const CGAffineTransform *m,
                  CGFloat x, CGFloat y,        // 圓心
                  CGFloat radius,              // 半徑
                  CGFloat startAngle,          // 起始弧度
                  CGFloat endAngle,            // 結束弧度
                  bool clockwise);             // 是否順時針

弧度的起算點是當前座標環境的 X 軸正方向,逆時針為正向(數學慣例)。

陷阱:iOS 的 Y 軸是反的

Quartz 的座標系原點在左下角、Y 軸向上;而 iOS 的 UIView 原點在左上角、Y 軸向下。UIView 在做 Quartz 繪圖時,已經對 Y 軸做了一次 Scale 為 -1 的轉換。

結果就是:最後那個 clockwise 參數的實際行為會與文件相反。你傳 YES(文件說是順時針),畫出來的卻是逆時針。

實驗一:clockwise 傳 NO

設定起點弧度 0、終點弧度 1.5π(即 270°),clockwise 傳 NO:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 建立 CGContextRef
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();

    // === 繪畫邏輯 ===
    // 用 Transform 轉移座標,就不必依實際顯示位置做計算
    CGAffineTransform transform = CGAffineTransformMakeTranslation(50, 50);

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathAddArc(path, &transform, 50, 50, 50, 0, 1.5 * M_PI, NO);

    CGPathMoveToPoint(path, &transform, 50, 0);
    CGPathAddLineToPoint(path, &transform, 50, 50);
    CGPathAddLineToPoint(path, &transform, 100, 50);

    // 把 path 加入當前 context
    CGContextAddPath(gc, path);

    [[UIColor grayColor] setFill];
    [[UIColor blueColor] setStroke];
    CGContextSetLineWidth(gc, 2);

    // 執行繪畫
    CGContextDrawPath(gc, kCGPathFillStroke);

    // 取出圖像並顯示
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:imgView];
}

輸出結果:

實際畫出的是順時針弧線——正好與 NO 的語意相反。

實驗二:clockwise 改成 YES

// 雖然傳的是 YES(文件定義為順時針),在 iOS 的 UIView 中這裡實際是逆時針
CGPathAddArc(path, &transform, 50, 50, 50, 0, 1.5 * M_PI, YES);

結果:

因為方向反了,從 0 到 1.5π 逆時針走只會畫出 1/4 圈的短弧。

CGPathAddArcToPoint:用兩條虛擬線定義弧

另一種畫弧方式是 CGPathAddArcToPoint。它的邏輯不同:由「當前點」與函式傳入的兩個點,構成兩條虛擬直線,然後畫出與這兩線相切、半徑指定的圓弧。

void CGPathAddArcToPoint(CGMutablePathRef path,
                         const CGAffineTransform *m,
                         CGFloat x1, CGFloat y1,   // 第一個控制點
                         CGFloat x2, CGFloat y2,   // 第二個控制點
                         CGFloat radius);          // 圓角半徑

弧線從當前點出發,畫到「圓與第二條線的交點」為止。這種定義方式在畫圓角矩形時特別方便——只要依序給出四角的外側控制點,就能一次連出完整的圓角框。

實作:用 CGPathAddArcToPoint 畫圓角矩形

// 建立 CGContextRef
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef gc = UIGraphicsGetCurrentContext();

// === 繪畫邏輯 ===
CGAffineTransform transform = CGAffineTransformMakeTranslation(100, 200);

CGMutablePathRef path = CGPathCreateMutable();

CGFloat radius = 10;                                        // 圓角半徑
CGPathMoveToPoint(path, &transform, 10, 0);                  // 起始點

// 右上角與右下角:畫出右側的兩個圓角
CGPathAddArcToPoint(path, &transform, 100, 0,   100, 100, radius);
CGPathAddArcToPoint(path, &transform, 100, 100, 0,   100, radius);

// 左下角與左上角:補完左側兩個圓角
CGPathAddArcToPoint(path, &transform, 0,   100, 0,   0,   radius);
CGPathAddArcToPoint(path, &transform, 0,   0,   100, 0,   radius);

// 加入 context 並繪製
CGContextAddPath(gc, path);
[[UIColor grayColor] setFill];
[[UIColor blueColor] setStroke];
CGContextSetLineWidth(gc, 2);
CGContextDrawPath(gc, kCGPathFillStroke);

// 取出圖像
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];

結果如下圖:

兩者該怎麼選

  • CGPathAddArc:已知圓心與半徑、要畫特定角度區間的弧時最直接,例如進度圓環、雷達掃描。
  • CGPathAddArcToPoint:以「折線轉圓角」的思維描述圖形時更方便,圓角矩形、圓角多邊形都是它的強項。

補充:Core Graphics 的記憶管理

範例中的 CGPathCreateMutable() 產生的物件採 Core Foundation 記憶體規則,必須自行釋放,否則會造成記憶體洩漏:

CGMutablePathRef path = CGPathCreateMutable();
// ... 使用 path ...
CGPathRelease(path);   // 別忘了這一行

另外,若要在 UIView 中常態繪圖,更推薦的做法是覆寫 drawRect: 並直接使用 UIGraphicsGetCurrentContext(),而不是像範例這樣先畫成 UIImage 再貼上——後者適合一次性的靜態圖產生,前者才能隨 setNeedsDisplay 重繪。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved