您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS繪圖基礎:Quartz2D圖形繪製完全教程

編輯:iOS 開發綜合

一、基本圖形繪製

Quartz2D能畫的東西相當豐富,包括:

* 線段(線寬、線段樣式)

* 矩形(空心、實心、顏色)

* 三角形、四邊形等各種形狀

1. drawRect: 方法說明

關於- (void)drawRect:(CGRect)rect,幾個重點要先記牢:

- 當 view 第一次被顯示時會呼叫一次。

- 或是重繪事件被觸發時才會呼叫。

- 千萬不要手動去呼叫這個方法。

- 若要觸發重繪,應改呼叫setNeedsDisplay或setNeedsDisplayInRect:。

2. 為什麼要在 drawRect: 中繪圖?

因為只有在這個方法裡,才能取得當前 View 的繪圖上下文(graphics context)。

/**

1. 當要向UIView上繪圖的時候, 必須重寫UIView的drawRect:方法, 然後在這個方法中進行繪圖

2. 在drawRect:方法中獲取的參數rect, 指的就是當前view的bounds屬性

3. 為什麼向當前view中繪制圖形, 必須在drawRect:方法中進行?

3.1 原因: 只有在當前view的drawRect:方法中才能成功的獲取當前view的"圖形上下文", 有了圖形上下文才能進行繪圖

3.2 為什麼只有在drawRect:方法中才能獲取當前view的圖形上下文呢?

3.3 原因: 是因為系統在調用drawRect:方法之前已經幫我們創建好了一個與當前view相關的圖形上下文了, 然後才調用的drawRect:方法, 所以在drawRect:方法中, 我們就可以成功獲取當前view的圖形上下文了。

3.4 如何創建一個圖形上下文?(後面說)

3.5 drawRect:方法是誰來調用的?什麼時候調用的?

3.5.1 drawRect:方法是系統幫我們調用的, 千萬別手動去調用這個方法。原因是, 自己手動去調用drawRect:方法的時候無法保證系統已經幫我們創建好了"圖形上下文", 所以這樣就無法保證在drawRect:方法中獲取"圖形上下文"對象, 所以也就無法繪圖。

3.5.2 drawRect:方法的什麼時候被調用?

1> 當這個View第一次顯示的時候會條用一次drawRect:方法。

2> 當這個View執行重繪操作的時候, 會重新調用drawRect:方法來進行繪圖。

3> 通過調用【[self setNeedsDisplay];// 把這個view都重繪一次】 或 【[self setNeedsDisplayInRect:(CGRect)]// 把view中的某個區域重繪一次】 來實現重繪

4> 在每次調用 setNeedsDisplay 或 setNeedsDisplayInRect:方法的時候, 內部會先創建一個與當前view相關連的"圖形上下文"然後再調用drawRect:實現重繪。

*/

案例實作

1. 繪製一根線段

/** 參考代碼:

void test1()

{

// 1. 獲取當前的圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 在上下文中繪制圖形(拼接路徑)

// 2.1 設置一個起點

CGContextMoveToPoint(ctx, 20, 20);

// 2.2 添加一條直線到(100, 100)這個點

CGContextAddLineToPoint(ctx, 100, 20);

// 3. 把上下文渲染顯示到 HMView01上

// StrokePath 表示把路徑以空心的形式渲染出來。

CGContextStrokePath(ctx);

}

*/

2. 繪製中文「二」(兩根線段)

/** 參考代碼:

void test2()

{

// 1. 獲取當前的圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 在上下文中繪制圖形(拼接路徑)

// 2.1 設置一個起點

CGContextMoveToPoint(ctx, 20, 20);

// 2.2 添加一條直線到(100, 100)這個點

CGContextAddLineToPoint(ctx, 100, 20);

// 2.3 再重新設置一個起點

CGContextMoveToPoint(ctx, 5, 50);

// 2.4 再添加一條線

CGContextAddLineToPoint(ctx, 115, 50);

// 3. 把上下文渲染顯示到 HMView01上

// StrokePath 表示把路徑以空心的形式渲染出來。

CGContextStrokePath(ctx);

}

*/

3. 繪製一個三角形

/** 參考代碼:

void test3()

{

// 1. 獲取當前的圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 在上下文中繪制圖形(拼接路徑)

// 2.1 設置一個起點

CGContextMoveToPoint(ctx, 20, 20);

// 2.2 添加一條直線到(100, 100)這個點

CGContextAddLineToPoint(ctx, 100, 100);

// 2.3 再添加一條線

CGContextAddLineToPoint(ctx, 120, 30);

// 2.4 再添加一條線段

//CGContextAddLineToPoint(ctx, 20, 20);

// 另外一種做法: 直接關閉路徑(連接最後一個點和起點)

CGContextClosePath(ctx);

// 3. 把上下文渲染顯示到 HMView01上

// StrokePath 表示把路徑以空心的形式渲染出來。

CGContextStrokePath(ctx);

}

*/

4. 繪製一個矩形(空心)

思路:可以用四根線段拼出一個矩形。

/** 參考代碼:

// 繪制四邊形, 這種畫法, 只能話"正"的圖形, "斜"的需要使用"矩陣"的方式來進行旋轉

void test4()

{

// 繪制一個"四邊形"

// 1. 獲取當前圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 開始繪制路徑

CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));

// 3. 渲染

CGContextStrokePath(ctx);

}

*/

5. 繪製一個實心矩形

/** 參考代碼:

// 繪制"實心"矩形, 只要把 Stroke 變成 Fill 即可

void test5()

{

// 1. 獲取當前圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 開始繪制路徑

CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));

// 3. 渲染

CGContextFillPath(ctx);

}

*/

6.1 設定圖形顏色

/** 參考代碼:

void test6()

{

// 1. 獲取當前圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 開始繪制路徑

CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));

//============ C 語言的方式設置顏色 =================

CGContextSetRGBFillColor(ctx, 200/255.0, 100/255.0, 50/255.0, 1.0);

//============ OC 的方式設置顏色 =================

// [[UIColor redColor] setStroke]; 設置空心圖形的線條顏色

// [[UIColor redColor] setFill]; 設置實心圖形的填充顏色

// [[UIColor redColor] set]; 統一設置空心與實心圖形的顏色

// 3. 渲染

CGContextFillPath(ctx);

}

*/

6.2 不同線段、不同顏色

/** 參考代碼:

void test8()

{

//畫兩根線, 一根紅色, 一根藍色

// 1. 獲取上下文對象

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 2. 繪制圖形

CGContextMoveToPoint(ctx, 50, 50);

CGContextAddLineToPoint(ctx, 50, 150);

[[UIColor redColor] set]; // 設置線段顏色

CGContextSetLineWidth(ctx, 10); // 設置線寬

CGContextStrokePath(ctx); // 渲染一次

CGContextMoveToPoint(ctx, 100, 50); // 再移動到新的起點

CGContextAddLineToPoint(ctx, 100, 150);

[[UIColor blueColor] set]; // 設置線的顏色

// 3. 渲染上下文對象到 view 上

CGContextStrokePath(ctx);

}

*/

7. 設定線段寬度(也能用來畫實心矩形)

/** 參考代碼:

void test7()

{

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));

[[UIColor redColor] set]; // 統一設置顏色

CGContextSetLineWidth(ctx, 20); // 設置線段寬度

CGContextStrokePath(ctx);

}

*/

8. 設定線段頭尾樣式

/** 參考代碼:

void test9()

{

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, 20, 20);

CGContextAddLineToPoint(ctx, 100, 100);

[[UIColor redColor] set];

CGContextSetLineWidth(ctx, 15);

//enum CGLineCap {

//kCGLineCapButt, 默認值

//kCGLineCapRound, 圓角

//kCGLineCapSquare 方角

//};

CGContextSetLineCap(ctx, kCGLineCapSquare);

CGContextStrokePath(ctx);

}

*/

9. 設定轉折點樣式

/** 參考代碼:

void test10()

{

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, 20, 20);

CGContextAddLineToPoint(ctx, 100, 100);

CGContextAddLineToPoint(ctx, 150, 50);

[[UIColor redColor] set];

CGContextSetLineWidth(ctx, 15);

CGContextSetLineCap(ctx, kCGLineCapRound); // 線段頭尾樣式

CGContextSetLineJoin(ctx, kCGLineJoinBevel); // 轉折點樣式

CGContextStrokePath(ctx);

}

*/

二、橢圓與圓

1. 橢圓與圓(圓其實就是矩形內的圖形)

/** 參考代碼:

void test()

{

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextAddEllipseInRect(ctx, CGRectMake(10, 20, 150, 100)); // 橢圓

CGContextAddEllipseInRect(ctx, CGRectMake(30, 50, 100, 100)); // 圓

CGContextStrokePath(ctx);

}

*/

2. 繪製圓環

思路1:畫圓時設定線寬即可,CGContextSetLineWidth(ctx, 20);。
思路2:畫兩個互相嵌套的圓。

3. 繪製各種圓弧

/** 參考代碼:

void test2()

{

CGContextRef ctx = UIGraphicsGetCurrentContext();

// ios 系統中坐標系剛好是反的所以指定了1(順時針)反而效果是"逆時針"。

//CGContextAddArc(ctx, 100, 100, 50, 0, M_PI, 0); 畫上面半個圓弧

//CGContextAddArc(ctx, 100, 100, 50, 0, M_PI * 0.5, 0);

//CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI_2, 0);

//CGContextAddArc(ctx, 100, 100, 50, M_PI, M_PI_2, 1); 畫左下角的1/4圓弧

//CGContextAddArc(ctx, 100, 100, 50, 0, M_PI * 2, 1); 畫一個圓

CGContextStrokePath(ctx);

}

*/

5. 畫一個左下角的1/4實心圓

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextAddArc(ctx, 100, 100, 50, M_PI, M_PI_2, 1); // 繪制左下角1/4圓弧

CGContextAddLineToPoint(ctx, 100, 100); // 添加一根線

CGContextClosePath(ctx); // 關閉路徑

CGContextFillPath(ctx);

}

*/

三、文字繪製(透過 OC 繪圖)

1. 繪製一段文字(直接使用 OC 方法,無需手動取得上下文)

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

NSString *str = @"哈哈, 黑馬程序員 iOS學院。";

NSDictionary *attrs = @{

NSForegroundColorAttributeName : [UIColor redColor],

NSFontAttributeName : [UIFont systemFontOfSize:20]

};

[str drawAtPoint:CGPointMake(30, 50) withAttributes:attrs];

}

*/

2. 繪製文字到指定區域(drawInRect:)

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

NSString *str = @"哈哈, 黑馬程序員 iOS。";

NSDictionary *attrs = @{

NSForegroundColorAttributeName : [UIColor redColor],

NSFontAttributeName : [UIFont systemFontOfSize:20]

};

[str drawInRect:CGRectMake(20, 20, 50, 250) withAttributes:attrs];

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextAddRect(ctx,CGRectMake(20, 20, 50, 250));

CGContextStrokePath(ctx);

}

*/

四、圖片繪製(透過 OC 繪圖)

1. 把圖片畫到 UIView 上

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

UIImage *img = [UIImage imageNamed:@"dst2"];

[img drawAtPoint:CGPointMake(0, 0)];

}

*/

2. 在指定矩形中繪製圖片(會自動拉伸)

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

UIImage *img = [UIImage imageNamed:@"dst2"];

[img drawInRect:CGRectMake(0, 0, 150, 150)];

}

*/

3. 畫格子花紋效果(pattern,使用 drawAsPatternInRect:)

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

UIImage *img = [UIImage imageNamed:@"abc"];

[img drawAsPatternInRect:self.bounds];

NSString *str = @"@傳智播客 ios 學院";

NSDictionary *attrs = @{

NSForegroundColorAttributeName : [UIColor redColor],

NSFontAttributeName : [UIFont systemFontOfSize:20]

};

[ str drawInRect:CGRectMake(0, 100, 200, 30) withAttributes:attrs];

}

*/

在指定區域畫圖片並疊加文字(浮水印效果)

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

UIImage *img = [UIImage imageNamed:@"dst2"];

[img drawInRect:self.bounds];

NSString *str = @"傳智播客 iOS 學院。";

[str drawInRect:CGRectMake(10, 400, 200, 30) withAttributes:nil];

}

*/

五、圖形上下文堆疊

情境:畫兩根線並各自設定顏色、寬度與頭尾樣式,但要求第二根線不沿用第一根線的樣式。做法是——在畫第一根線之前先保存繪圖上下文堆疊,畫第二根線時再恢復:

CGContextSaveGState(ctx);// 保存 ctx 這個繪圖上下文物件
CGContextRestoreGState(ctx); // 恢復 ctx 這個圖形上下文

六、矩陣操作

1. 整體旋轉、整體縮放

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

CGContextRef ctx = UIGraphicsGetCurrentContext();

//======================= 矩陣操作 ============================

CGContextRotateCTM(ctx, M_PI_4 * 0.5); // 旋轉

CGContextScaleCTM(ctx, 0.5, 0.5); // 縮放

//======================= 矩陣操作 ============================

CGContextMoveToPoint(ctx, 10, 10);

CGContextAddLineToPoint(ctx, 100, 100);

CGContextAddEllipseInRect(ctx, CGRectMake(130, 150, 100, 100));

CGContextAddRect(ctx, CGRectMake(70, 90, 100, 80));

CGContextStrokePath(ctx);

}

*/

2. 讓最後畫的圖形不受矩陣操作影響

思路:繪圖前先保存上下文,畫最後一個圖形前再恢復上下文。

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSaveGState(ctx); // 保存上下文

CGContextRotateCTM(ctx, M_PI_4 * 0.5);

CGContextScaleCTM(ctx, 0.5, 0.5);

CGContextMoveToPoint(ctx, 10, 10);

CGContextAddLineToPoint(ctx, 100, 100);

CGContextAddEllipseInRect(ctx, CGRectMake(130, 150, 100, 100));

CGContextRestoreGState(ctx); // 恢復上下文

CGContextAddRect(ctx, CGRectMake(70, 90, 100, 80));

CGContextStrokePath(ctx);

}

*/

七、圖片裁剪(把圖片裁成圓形)

思路:在 UIView 上先畫一個圓,接著裁剪上下文,最後把圖片繪製到上下文上,超出圓形的部分自然就被裁掉了。

/** 參考代碼:

- (void)drawRect:(CGRect)rect {

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextAddArc(ctx, 100, 100, 90, 0, M_PI * 2, 1); // 1. 畫圓

CGContextClip(ctx); // 2. 裁剪上下文,之後只能在裁剪區域內繪圖

UIImage *img = [UIImage imageNamed:@"dst2"]; // 3. 把圖片繪製上去

[img drawAtPoint:CGPointMake(0, 0)];

}

*/

八、重繪案例:滑桿控制圓的大小

思路:讓繪製圓時的半徑隨 slider 的變化而改變。setNeedsDisplay會清除舊內容再重繪,setNeedsDisplayInRect:則是局部刷新。

/** 參考代碼:

- (void)setRadius:(CGFloat)radius

{

_radius = radius;

[self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextAddArc(ctx, 100, 100, self.radius, 0, M_PI * 2, 1);

CGContextFillPath(ctx);

}

*/

九、模仿 UIImageView

思路:自訂一個繼承自 UIView 的類別,加上 image 屬性,在 setter 中觸發重繪即可。

/** 參考代碼:

// 1. 控制器

- (void)viewDidLoad {

[super viewDidLoad];

MyImageView *imgView =[[MyImageView alloc] init];

imgView.image = [UIImage imageNamed:@"dst2"];

imgView.frame = CGRectMake(30, 30, 100, 100);

[self.view addSubview:imgView];

self.myImgView = imgView;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

self.myImgView.image = [UIImage imageNamed:@"snow"];

}

// 2. 自定義 view

- (void)setImage:(UIImage *)image

{

_image = image;

[self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

[self.image drawAtPoint:CGPointZero];

}

*/

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved