ios 圖形與動(dòng)畫(huà)學(xué)習(xí)筆記 構(gòu)造路徑

ios 圖形與動(dòng)畫(huà)學(xué)習(xí)筆記 構(gòu)造路徑(CGPathCreateMutable)

一系列點(diǎn)放在一起,構(gòu)成了一個(gè)形狀。一系列的形狀放在一起,構(gòu)成了一個(gè)路徑。

/*

路徑屬于我們正在繪制他們的上下文。路徑?jīng)]有邊界(Boundary)或特定的形狀,不想我們使用路徑繪制出來(lái)的形狀。

但路徑?jīng)]有邊界框(Bounding boxes).此處,Boundary與Bounding boxes完全不一樣。

邊界顯示你在畫(huà)布上哪些不可以用來(lái)繪畫(huà),而路徑的邊界框是包含了所有路徑的形狀、點(diǎn)和其他已經(jīng)繪制的對(duì)象的最小矩形。

使用路徑創(chuàng)建步驟:創(chuàng)建路徑的方法返回一個(gè)路徑的句柄,可以在繪制圖形的使用就可以把句柄作為傳遞給core Graphics。

當(dāng)創(chuàng)建路徑之后,可以向它添加不同的點(diǎn)、線條和形狀,之后繪制圖形。

1、CGPathCreateMutable函數(shù)

創(chuàng)建一個(gè)CGMutablePathRef的可變路徑,并返回其句柄。

2、CGPathMoveToPoint過(guò)程

在路徑上移動(dòng)當(dāng)前畫(huà)筆的位置到一個(gè)點(diǎn),這個(gè)點(diǎn)由CGPoint類型的參數(shù)指定。

3、CGPathAddLineToPoint過(guò)程

從當(dāng)前的畫(huà)筆位置向指定位置(同樣由CGPoint類型的值指定)繪制線段

4、CGContextAddPath過(guò)程

添加一個(gè)由句柄指定的路徑的圖形上下文,準(zhǔn)備用于繪圖

5、CGContextDrawPath過(guò)程

在圖形上下文中繪制給出的路徑。

6、CGPathRelease過(guò)程

釋放為路徑句柄分配的內(nèi)存。

7、CGPathAddRect過(guò)程

向路徑添加一個(gè)矩形。矩形的邊界由一個(gè)CGRect結(jié)構(gòu)體指定。

*/

/*

*創(chuàng)建一個(gè)新的可變路徑(CGPathCreateMutable),把該路徑加到你的圖形上下文(CGContextAddPath)

*并把它繪制到圖形上下文中(CGContextDrawPath)

*/

具體代碼:

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

/*

*創(chuàng)建一個(gè)新的可變路徑(CGPathCreateMutable),把該路徑加到你的圖形上下文(CGContextAddPath)

*并把它繪制到圖形上下文中(CGContextDrawPath)

*/

/* Create the path */

CGMutablePathRefpath =CGPathCreateMutable();

/* How big is our screen? We want the X to cover the whole screen */

CGRectscreenBounds = [[UIScreenmainScreen]bounds];

/* Start from top-left */

CGPathMoveToPoint(path,NULL,screenBounds.origin.x, screenBounds.origin.y);

/* Draw a line from top-left to bottom-right of the screen */

CGPathAddLineToPoint(path,NULL,screenBounds.size.width, screenBounds.size.height);

/* Start another line from top-right */

CGPathMoveToPoint(path,NULL,screenBounds.size.width, screenBounds.origin.y);

/* Draw a line from top-right to bottom-left */

CGPathAddLineToPoint(path,NULL,screenBounds.origin.x, screenBounds.size.height);

/* Get the context that the path has to be drawn on */

CGContextRefcurrentContext =UIGraphicsGetCurrentContext();

/* Add the path to the context so we can draw it later */

CGContextAddPath(currentContext, path);

/* Set the blue color as the stroke color */

[[UIColorblueColor]setStroke];

/* Draw the path with stroke color */

CGContextDrawPath(currentContext,kCGPathStroke);

/* Finally release the path object */

CGPathRelease(path);

/*

*傳入CGPathMoveToPoint等過(guò)程的NULL參數(shù)代表一個(gè)既定的變換,在給定的路徑繪制線條時(shí)可以使用此變換。

*/

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 18- UIBezierPath官方API中文翻譯(待校對(duì)) ----------------- 華麗的分割線 -...
    醉臥欄桿聽(tīng)雨聲閱讀 1,167評(píng)論 1 1
  • --繪圖與濾鏡全面解析 概述 在iOS中可以很容易的開(kāi)發(fā)出絢麗的界面效果,一方面得益于成功系統(tǒng)的設(shè)計(jì),另一方面得益...
    韓七夏閱讀 2,975評(píng)論 2 10
  • Quartz 2D是二維圖形繪制引擎,可以實(shí)現(xiàn)N多圖形圖像的操作功能,如基本路徑的繪制、透明度、描影、繪制陰影、透...
    起名好難_fz閱讀 492評(píng)論 0 2
  • /* 路徑屬于我們正在繪制他們的上下文。路徑?jīng)]有邊界(Boundary)或特定的形狀,不想我們使用路徑繪制出來(lái)的形...
    wintersal閱讀 985評(píng)論 0 51
  • 今天家人中秋團(tuán)聚, 少年餐後提出離開(kāi)。 到家後電話回家線路、交通方式。 回家後午休, 電話進(jìn)度和安排, 運(yùn)動(dòng)完後,...
    千吉change閱讀 219評(píng)論 0 0

友情鏈接更多精彩內(nèi)容