1. 概念
二維的繪圖庫。當控件樣式復(fù)雜時,可以把控件畫出來,就是自定義控件.
2. 基礎(chǔ)繪制流程
在drawRect方法中實現(xiàn)
- 獲取圖形上下文
?CGContextRef ctx = UIGraphicsGetCurrentContext(); - 畫貝塞爾曲線,可以畫各種形狀等
?UIBezierPath *path = [self arcBezierPath]; - 路徑添加到上下文中
?CGContextAddPath(ctx, path.CGPath); - 渲染到視圖上
?CGContextStrokePath(ctx);
3. 基礎(chǔ)繪畫流程簡化
前兩步一樣,第三部直接用貝塞爾路徑渲染。它底層的實現(xiàn),就是獲取上下文,拼接路徑,把路徑添加到上下文,渲染到View
- 畫路徑,可以是線條曲線矩形圓等
UIBezierPath *path = [self arcBezierPath]; - 直接用貝塞爾曲線繪制或填充
[path stroke];
4. 下載進度條
用到了定時器CADisplayLink,刷新頻率60hz,也就是1秒鐘會執(zhí)行60次指定的selector
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
繪制的方法中,調(diào)整endAngle,就會出現(xiàn)動態(tài)的效果
UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(125, 125) radius:100 startAngle:0 endAngle:M_PI*2*self.proValue clockwise:YES];
[arcPath setLineWidth:25];
[[[UIColor blueColor] colorWithAlphaComponent:0.6] set];
[arcPath stroke];

5. 畫餅圖
用貝塞爾畫圓弧,添加直線到圓心,設(shè)置顏色[[UIColor redColor] setFill],最后[path fill]

6. UIKit繪圖
這部分比較簡單,文字和圖片的繪制

7. 圖像上下文生成新的圖片
有著特定的步驟
開啟圖形上下文
?UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);-
可以添加裁剪區(qū)域,然后執(zhí)行繪制圖片或者渲染layer等操作
//裁剪區(qū)域 UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, yellow.size.width, yellow.size.height)]; [clipPath addClip]; //圖片繪制 [yellow drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1.0]; //渲染layer [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 圖片上下文當中生成一張圖片
?UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();關(guān)閉上下文
?UIGraphicsEndImageContext();
8. 圖像擦除
CGContextClearRect(ctx, rect);

9. 其他Tips
-
查詢當前CGPoint或者CGRect是否在一個Frame內(nèi)
CGRectContainsRect(button.frame, locationrect) -
大作業(yè),制作一個繪畫板,用到的東西就是最基礎(chǔ)的東西,只要分步驟,一步一步實現(xiàn),就可以完成,大家試著自己做出來
?