Quartz2D能畫的東西相當豐富,包括:
* 線段(線寬、線段樣式)
* 矩形(空心、實心、顏色)
* 三角形、四邊形等各種形狀
關於- (void)drawRect:(CGRect)rect,幾個重點要先記牢:
- 當 view 第一次被顯示時會呼叫一次。
- 或是重繪事件被觸發時才會呼叫。
- 千萬不要手動去呼叫這個方法。
- 若要觸發重繪,應改呼叫setNeedsDisplay或setNeedsDisplayInRect:。
因為只有在這個方法裡,才能取得當前 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:實現重繪。
*/
/** 參考代碼:
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);
}
*/
/** 參考代碼:
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);
}
*/
/** 參考代碼:
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);
}
*/
思路:可以用四根線段拼出一個矩形。
/** 參考代碼:
// 繪制四邊形, 這種畫法, 只能話"正"的圖形, "斜"的需要使用"矩陣"的方式來進行旋轉
void test4()
{
// 繪制一個"四邊形"
// 1. 獲取當前圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2. 開始繪制路徑
CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));
// 3. 渲染
CGContextStrokePath(ctx);
}
*/
/** 參考代碼:
// 繪制"實心"矩形, 只要把 Stroke 變成 Fill 即可
void test5()
{
// 1. 獲取當前圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2. 開始繪制路徑
CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));
// 3. 渲染
CGContextFillPath(ctx);
}
*/
/** 參考代碼:
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);
}
*/
/** 參考代碼:
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);
}
*/
/** 參考代碼:
void test7()
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));
[[UIColor redColor] set]; // 統一設置顏色
CGContextSetLineWidth(ctx, 20); // 設置線段寬度
CGContextStrokePath(ctx);
}
*/
/** 參考代碼:
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);
}
*/
/** 參考代碼:
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);
}
*/
/** 參考代碼:
void test()
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, CGRectMake(10, 20, 150, 100)); // 橢圓
CGContextAddEllipseInRect(ctx, CGRectMake(30, 50, 100, 100)); // 圓
CGContextStrokePath(ctx);
}
*/
思路1:畫圓時設定線寬即可,CGContextSetLineWidth(ctx, 20);。
思路2:畫兩個互相嵌套的圓。
/** 參考代碼:
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);
}
*/
/** 參考代碼:
- (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);
}
*/
/** 參考代碼:
- (void)drawRect:(CGRect)rect {
NSString *str = @"哈哈, 黑馬程序員 iOS學院。";
NSDictionary *attrs = @{
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:20]
};
[str drawAtPoint:CGPointMake(30, 50) withAttributes:attrs];
}
*/
/** 參考代碼:
- (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);
}
*/
/** 參考代碼:
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"dst2"];
[img drawAtPoint:CGPointMake(0, 0)];
}
*/
/** 參考代碼:
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"dst2"];
[img drawInRect:CGRectMake(0, 0, 150, 150)];
}
*/
/** 參考代碼:
- (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 這個圖形上下文
/** 參考代碼:
- (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);
}
*/
思路:繪圖前先保存上下文,畫最後一個圖形前再恢復上下文。
/** 參考代碼:
- (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);
}
*/
思路:自訂一個繼承自 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];
}
*/