IOS開發(80)之畫線

來源:互聯網
上載者:User

1 前言
得到圖形環境,然後用 CGContextMoveToPoint 和 CGContextAddLineToPoint 函數來畫線。


2 代碼執行個體
ZYViewControllerView.m

 

[plain]
- (void)drawRect:(CGRect)rect{ 
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 40.0f) textToDisplay:@"Miter" 
                         lineJoin:kCGLineJoinMiter]; 
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 180.0f) textToDisplay:@"Bevel" 
                         lineJoin:kCGLineJoinBevel]; 
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 320.0f) textToDisplay:@"Round" 
                        lineJoin:kCGLineJoinRound]; 

/* 
 paramTopPoint:一個點,頂部在這一點 
 textToDisplay:內顯示的文字 
 lineJoin:要使用的接合類型 
        kCGLineJoinMiter 
        接合點為尖角。這是預設的接合類型。 
        kCGLineJoinBevel 
        接合點為斜角 
        kCGLineJoinRound 
        接合點為圓角 
 */ 
- (void) drawRooftopAtTopPointof:(CGPoint)paramTopPoint textToDisplay:(NSString *)paramText 
                        lineJoin:(CGLineJoin)paramLineJoin{ 
    /*設定線條顏色*/ 
    [[UIColor brownColor] set]; 
    //獲得當前圖形上下文 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    //設定連線類型 
    CGContextSetLineJoin(currentContext, paramLineJoin); 
    //設定線條寬度 
    CGContextSetLineWidth(currentContext,20.0f); 
    //設定開始點位置 
    CGContextMoveToPoint(currentContext,paramTopPoint.x - 140, paramTopPoint.y + 100); 
    //設定終點 
    CGContextAddLineToPoint(currentContext,paramTopPoint.x, paramTopPoint.y); 
    //設定另一個終點 
    CGContextAddLineToPoint(currentContext,paramTopPoint.x + 140, paramTopPoint.y + 100); 
    //畫線 
    CGContextStrokePath(currentContext); 
    [[UIColor blackColor] set]; 
    /* 寫文字 */ 
    CGPoint drawingPoint = CGPointMake(paramTopPoint.x - 40.0f, 
                                   paramTopPoint.y + 60.0f); 
    [paramText drawAtPoint:drawingPoint withFont:[UIFont boldSystemFontOfSize:30.0f]]; 

聯繫我們

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