iOS進階-QuartzCore架構-2D繪圖

來源:互聯網
上載者:User

標籤:

一、理論知識
什麼是Quartz2D

Quartz2D執行個體


Quartz2D在iOS開發中的價值

圖形上下文

自訂View

drawRect:方法

繪圖順序(後蓋前)

Quartz2D須知

 

二、畫線段
1.建立一個類MJLineView,繼承自UIView。
2.拖一個UIView,Class為MJLineView
3.在drawRect:方法裡畫圖
-(void)drawRect:(CGRect)rect
{
//1.獲得圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

//2.拼接圖形(路徑)
//設定一個起點(之所以在參數中傳上下文是為了把點存到上下文中)
CGContextMoveToPoint(ctx,10,10);
//添加一條線段到點(100,100)
CGContextAddLineToPoint(ctx,100,100);
//再添加一條線段[會從(100,100)繼續畫]
CGContextAddLineToPoint(ctx,150,40);
//3.繪製圖形(渲染顯示到view上面)
CGContextStrokePath(ctx);//空心
// CGContextFillPath(ctx); //實心
}

 


三、畫形狀(三角形,矩形等)
1.建立一個類MJShapeView,繼承自UIView。
2.拖一個UIView,Class為MJShapeView
3.在drawRect:方法裡畫圖
-(void)drawRect:(CGRect)rect
{
drawRect();
}


//畫四邊形(缺點:只能畫水平的四邊形,不能畫斜的)
void draw4Rect()
{
//1.獲得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

//2.畫矩形
CGContextAddRect(ctx,CGRectMake(10,10,100,100));

//3.繪製圖形
CGContextStrokePath(ctx);
}
//畫三角形
void drawTriangle()
{
//1.獲得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

//2.畫三角形
CGContextMoveToPoint(ctx,0,0);
CGContextAddLineToPoint(ctx,100,100);
CGContextAddLineToPoint(ctx,150,80);

//關閉路徑(串連起點和最後一個點)
CGContextClosePath(ctx);
//3.繪製圖形
CGContextStrokePath(ctx);
}

iOS進階-QuartzCore架構-2D繪圖

聯繫我們

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