標籤:
一、理論知識
什麼是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繪圖