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í)可以使用此變換。
*/
}