1 前言
構造和繪製路徑,能夠在圖形環境上畫任意形狀.
2 代碼執行個體
ZYViewControllerView.m
[plain] - (void)drawRect:(CGRect)rect{
//建立路徑 建立一個新的 CGMutablePathRef 類型的可變路徑並返回其控制代碼。
CGMutablePathRef path = CGPathCreateMutable();
/* How big is our screen? We want the X to cover the whole screen */
//範圍為整個螢幕
CGRect screenBounds = [[UIScreen mainScreen] bounds];
//從左上方開始畫路徑 將路徑上當前畫筆位置移動到 CGPoint 類型的參數指定的點。
CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y);
//從左上方連線到右下角 從畫筆當前位置向指定位置繪製一條線段。
CGPathAddLineToPoint(path,NULL, screenBounds.size.width, screenBounds.size.height);
//開始另一點從右上方
CGPathMoveToPoint(path,NULL, screenBounds.size.width, screenBounds.origin.y);
//從右上方到左下角
CGPathAddLineToPoint(path,NULL, screenBounds.origin.x, screenBounds.size.height);
//獲得當前圖形的上下文
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Add the path to the context so we candraw it later */
//添加路徑到路徑上下文中 向圖形環境上添加一個路徑(由一個路徑控制代碼指定),該路徑已經準備好被繪製。
CGContextAddPath(currentContext,path);
//設定藍色
[[UIColor blueColor] setStroke];
//畫圖 在圖形環境上繪製指定路徑
/*kCGPathStroke
畫線來標記路徑的邊界或邊緣,使用選中的繪圖色。
kCGPathFill
用選中的填充色,填充被路徑包圍的地區。
kCGPathFillStroke
組合繪圖和填充。用當前填充色填充路徑,並用當前繪圖色繪製路徑邊界。下面我們會看到一個使用此方 法的例子。
*/
CGContextDrawPath(currentContext, kCGPathStroke);
//釋放路徑
CGPathRelease(path);
}
- (void)drawRect:(CGRect)rect{
//建立路徑 建立一個新的 CGMutablePathRef 類型的可變路徑並返回其控制代碼。
CGMutablePathRef path = CGPathCreateMutable();
/* How big is our screen? We want the X to cover the whole screen */
//範圍為整個螢幕
CGRect screenBounds = [[UIScreen mainScreen] bounds];
//從左上方開始畫路徑 將路徑上當前畫筆位置移動到 CGPoint 類型的參數指定的點。
CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y);
//從左上方連線到右下角 從畫筆當前位置向指定位置繪製一條線段。
CGPathAddLineToPoint(path,NULL, screenBounds.size.width, screenBounds.size.height);
//開始另一點從右上方
CGPathMoveToPoint(path,NULL, screenBounds.size.width, screenBounds.origin.y);
//從右上方到左下角
CGPathAddLineToPoint(path,NULL, screenBounds.origin.x, screenBounds.size.height);
//獲得當前圖形的上下文
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Add the path to the context so we candraw it later */
//添加路徑到路徑上下文中 向圖形環境上添加一個路徑(由一個路徑控制代碼指定),該路徑已經準備好被繪製。
CGContextAddPath(currentContext,path);
//設定藍色
[[UIColor blueColor] setStroke];
//畫圖 在圖形環境上繪製指定路徑
/*kCGPathStroke
畫線來標記路徑的邊界或邊緣,使用選中的繪圖色。
kCGPathFill
用選中的填充色,填充被路徑包圍的地區。
kCGPathFillStroke
組合繪圖和填充。用當前填充色填充路徑,並用當前繪圖色繪製路徑邊界。下面我們會看到一個使用此方 法的例子。
*/
CGContextDrawPath(currentContext, kCGPathStroke);
//釋放路徑
CGPathRelease(path);
}
隱藏狀態條
此例中,我要隱藏程式的狀態條:請找到 Info.plist,並增加一個 UIStatusBarHidden 鍵,將其值設定為 YES