ios開發日記 10- CGPathAddArc和CGPathAddArcToPoint函數

來源:互聯網
上載者:User

標籤:

http://www.cnblogs.com/mgen/p/3426303.html

注意:CGPathAddArc和CGContextAddArc,以及UIBezierPath類型的addArcWithCenter:radius:startAngle:endAngle:clockwise:方法原理都類似。而CGPathAddArcToPoint和CGContextAddArcToPoint函數則原理類似。只不過CGPath相關的函數多了一個CGAffineTransform參數,可以直接應用Transform,對於CGContextAddArc和CGContextAddArcToPoint函數,Transform可以使用CGContextXxxCTM方法,之前也寫過一篇文章:iOS Quartz: 使用CGContextRef中的Transform來輔助作圖。

 

切入正題,我們首先來看CGPathAddArc函數,我認為CGPathAddArc函數比CGPathAddArcToPoint函數更好理解。可以參考這個SO回答的。CGPathAddArc函數是通過圓心和半徑定義一個圓,然後通過兩個弧度確定一個弧線。注意弧度是以當前座標環境的X軸開始的。

需要注意的是由於iOS中的座標體系是和Quartz座標體系中Y軸相反的,所以iOS UIView在做Quartz繪圖時,Y軸已經做了Scale為-1的轉換,因此造成CGPathAddArc函數最後一個是否是順時針的參數結果正好是相反的,也就是說如果設定最後的參數為YES,根據參數定義應該是順時針的,但實際繪圖結果會是逆時針的!

 

比如,我們設定起點弧度為0,終點弧度為1.5 * PI(等於270角度),然後最後的clockwise參數為NO,代碼:

- (void)viewDidLoad

{

    [super viewDidLoad];

   

    //建立CGContextRef

    UIGraphicsBeginImageContext(self.view.bounds.size);

    CGContextRef gc = UIGraphicsGetCurrentContext();

   

    //=== 繪畫邏輯 ===

    //建立用於轉移座標的Transform,這樣我們不用按照實際顯示做座標計算

    CGAffineTransform transform = CGAffineTransformMakeTranslation(50, 50);

    //建立CGMutablePathRef

    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);

   

    //將CGMutablePathRef添加到當前Context內

    CGContextAddPath(gc, path);

    [[UIColor grayColor] setFill];

    [[UIColor blueColor] setStroke];

    CGContextSetLineWidth(gc, 2);

    //執行繪畫

    CGContextDrawPath(gc, kCGPathFillStroke);

   

    //從Context中擷取映像,並顯示在介面上

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   

    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

    [self.view addSubview:imgView];

}

 

輸出:

結果會是順時針繪製弧線.

如果把CGPathAddArc函數改成這樣:

//雖然順時針參數是YES,在iOS中的UIView中,這裡實際是逆時針。所以只會畫出1/4。

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

 

雖然順時針參數是YES,在iOS中,這裡實際是逆時針。所以只會畫出1/4。結果會是:

 

而CGContextAddArcToPoint函數則是另一種繪製弧線的方式,同樣可以參考那個SO回答的.它是通過畫兩個虛擬線來完成繪圖的,這兩條線是通過當前CGContextRef的點,和CGContextAddArcToPoint函數本身定義的兩個點來完成的。而弧線會從當前CGContextRef的點開始,畫到中心圓與第二條線的交點處。這樣的畫弧方式,在某些情況下可以使CGContextAddArcToPoint函數比CGPathAddArc用起來更加方便些。比如花圓角矩形。

 

樣本,我們來完成這樣一個繪圖:

 

使用CGContextAddArcToPoint函數,我們就可以不去關心太多花弧時的弧度相關資料,而只需要指定矩形的頂點座標,因為這個弧線是依賴兩個頂點座標所圍成的空間的,這正是CGContextAddArcToPoint發揮作用的地方。實現代碼:

- (void)viewDidLoad

{

    [super viewDidLoad];

   

    //建立CGContextRef

    UIGraphicsBeginImageContext(self.view.bounds.size);

    CGContextRef gc = UIGraphicsGetCurrentContext();

   

    //=== 繪畫邏輯 ===

    //建立用於轉移座標的Transform,這樣我們不用按照實際顯示做座標計算

    CGAffineTransform transform = CGAffineTransformMakeTranslation(50, 50);

    //建立CGMutablePathRef

    CGMutablePathRef path = CGPathCreateMutable();

    //半徑為30

    CGFloat radius = 30;

    //初始點為(0, 0)

    CGPathMoveToPoint(path, &transform, 0, 0);

    //右上方和右下角兩個點,畫出半個圓角

    CGPathAddArcToPoint(path, &transform, 100, 0, 100, 100, radius);

    //右下角和左下角兩個點,畫出另外半個圓角

    CGPathAddArcToPoint(path, &transform, 100, 100, 0, 100, radius);

    //CGPathAddArcToPoint會在交點中停止,所以需要再次調用CGPathAddLineToPoint畫出下面的線

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

   

    //將CGMutablePathRef添加到當前Context內

    CGContextAddPath(gc, path);

    [[UIColor grayColor] setFill];

    [[UIColor blueColor] setStroke];

    CGContextSetLineWidth(gc, 2);

    //執行繪畫

    CGContextDrawPath(gc, kCGPathFillStroke);

   

    //從Context中擷取映像,並顯示在介面上

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   

    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

    [self.view addSubview:imgView];

}

ios開發日記 10- CGPathAddArc和CGPathAddArcToPoint函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.