IOS開發(81)之構造路徑

來源:互聯網
上載者:User

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


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.