0 CGContextRef context = UIGraphicsGetCurrentContext(); 設置上下文 1 CGContextMoveToPoint 開始畫線 2 CGContextAddLineToPoint 畫直線 4 CGContextAddEllipseInRect 畫一橢圓 4 CGContextSetLineCap 設置線條終點形狀 4 CGContextSetLineDash 畫虛線 4 CGContextAddRect 畫一方框 4 CGContextStrokeRect 指定矩形 4 CGContextStrokeRectWithWidth 指定矩形線寬度 4 CGContextStrokeLineSegments 一些直線 5 CGContextAddArc 畫一曲線 前2點爲中心 中間倆店爲起始弧度 最後一數據爲0則順時針畫1則逆時針 5 CGContextAddArcToPoint(context,0,0, 2, 9, 40);先畫倆條線從point 到第1點 從第1點到第2點的線 切割裏面的圓 6 CGContextSetShadowWithColor 設置陰影 顏色 7 CGContextSetRGBFillColor 設置填充顏色 7 CGContextSetRGBStrokeColor 設置畫筆/邊框顏色 7 CGContextSetFillColorSpace 顏色空間填充 7 CGConextSetStrokeColorSpace 顏色空間畫筆設置 8 CGContextFillRect 補充當前填充顏色的rect 8 CGContextSetAlaha 透明度 9 CGContextTranslateCTM 改變畫布位置 10 CGContextSetLineWidth 設置線的寬度 11 CGContextAddRects 畫多個線 12 CGContextAddQuadCurveToPoint 畫曲線 13 CGContextStrokePath 開始繪製圖片 13 CGContextDrawPath 設置繪製模式 14 CGContextClosePath 封閉當前線路 15 CGContextTranslateCTM(context, 0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0);反轉畫布 16 CGContextSetInterpolationQuality 背景內置顏色質量等級 16 CGImageCreateWithImageInRect 從原圖片中取小圖 17 字符串的寫入可用 NSString自己的畫圖方法 - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode: (UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment; 18對圖片放大縮小的功能就是慢了點 UIGraphicsBeginImageContext(newSize); UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 19 CGColorGetComponents() 返回顏色的各個直 以及透明度 可用只讀const float 來接收 是個數組 20 畫圖片 CGImageRef image=CGImageRetain(img.CGImage); CGContextDrawImage(context, CGRectMake(10.0, height - 100.0, 90.0, 90.0), image); 21 實現逐變顏色填充方法 CGContextClip(context); CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); CGFloat colors[] = { 204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00, 0.0 / 255.0, 50.0 / 255.0, 126.0 / 255.0, 1.00, }; CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); CGColorSpaceRelease(rgb); CGContextDrawLinearGradient(context, gradient,CGPointMake(0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),kCGGradientDrawsBeforeStartLocation); 22 注: 畫完圖後,必須先用CGContextStrokePath來描線,即形狀後用CGContextFillPath來填充形狀內的顏色. 填充一個路徑的時候,路徑裏面的子路徑都是獨立填充的。 假如是重疊的路徑,決定一個點是否被填充,有兩種規則 1,nonzero winding number rule:非零繞數規則,假如一個點被從左到右跨過,計數器+1,從右到左跨過,計數器-1,最後,若是結果是0,那麼不填充,若是是非零,那麼填充。 2,even-odd rule: 奇偶規則,假如一個點被跨過,那麼+1,最後是奇數,那麼要被填充,偶數則不填充,和方向沒有關係。 CGContextEOFillPath 使用奇偶規則填充當前路徑 CGContextFillPath 使用非零繞數規則填充當前路徑 CGContextFillRect 填充指定的矩形 CGContextFillRects 填充指定的一些矩形 CGContextFillEllipseInRect 填充指定矩形中的橢圓 CGContextDrawPath 兩個參數決定填充規則, kCGPathFill表示用非零繞數規則, kCGPathEOFill表示用奇偶規則, kCGPathFillStroke表示填充, kCGPathEOFillStroke表示描線,不是填充 當一個顏色覆蓋上另一個顏色,兩個顏色怎麼混合 默認方式是 result = (alpha * foreground) + (1 - alpha) * background CGContextSetBlendMode :設置blend mode. CGContextSaveGState :保存blend mode. CGContextRestoreGState:在沒有保存以前,用這個函數還原blend mode. CGContextSetBlendMode 混合倆種顏色
以上引用php
======================================================================html
======================================================================數組
畫虛線須要用到函數:函數
CG_EXTERN void CGContextSetLineDash(CGContextRef __nullable c, CGFloat phase, const CGFloat * __nullable lengths, size_t count) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
此函數須要四個參數:
context – 這個不用多說
phase - 稍後再說
lengths – 指明虛線是如何交替繪製,具體看例子
count – lengths數組的長度ui
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath(context); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, [UIColorwhiteColor].CGColor); float lengths[] = {10,10}; CGContextSetLineDash(context, 0, lengths,2); CGContextMoveToPoint(context, 10.0, 20.0); CGContextAddLineToPoint(context, 310.0,20.0); CGContextStrokePath(context); CGContextClosePath(context);
lengths的值{10,10}表示先繪製10個點,再跳過10個點,如此反覆,如圖:spa
若是把lengths值改成{10, 20, 10},則表示先繪製10個點,跳過20個點,繪製10個點,跳過10個點,再繪製20個點,如此反覆,如圖:.net
注意count的值等於lengths數組的長度3d
phase參數表示在第一個虛線繪製的時候跳過多少個點,舉例說明:code
float lengths[] = {10,5}; CGContextSetLineDash(context, 0, lengths, 2); CGContextMoveToPoint(context, 0.0, 20.0); CGContextAddLineToPoint(context, 310.0, 20.0); CGContextStrokePath(context); ======== CGContextSetLineDash(context, 5, lengths, 2); CGContextMoveToPoint(context, 0.0, 40.0); CGContextAddLineToPoint(context, 310.0, 40.0); CGContextStrokePath(context); ======== CGContextSetLineDash(context, 8, lengths, 2); CGContextMoveToPoint(context, 0.0, 60.0); CGContextAddLineToPoint(context, 310.0, 60.); CGContextStrokePath(context);
如圖顯示:
因爲lengths值爲{10,5},第一條線就是繪製10,跳過5,反覆繪製。
第二條線的phase值爲5,則首先繪製【10減去5】,再跳過5,繪製10,反覆繪製。
第三條給也如此,先繪製2,再跳過5,如此反覆。htm
================================================================
切線
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGContextMoveToPoint(context, 100, 100);//起始點 CGContextAddArcToPoint(context, 100,200, 300,200, 100);//點1,點2,半徑 CGContextStrokePath(context); }
================================================================
橢圓
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGRect rectangle = CGRectMake(60,170,200,80); CGContextAddEllipseInRect(context, rectangle); CGContextStrokePath(context); }
================================================================
曲線
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGContextMoveToPoint(context, 10, 10);//起始點 CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);//控制點1,控制點2,終點 CGContextStrokePath(context); }
================================================================
曲線
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGContextMoveToPoint(context, 10, 200); CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);//控制點1,終點 CGContextStrokePath(context); }
================================================================
虛線曲線
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 5.0); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGFloat dashArray[] = {2,6,4,2}; CGContextSetLineDash(context, 3, dashArray, 4); CGContextMoveToPoint(context, 10, 200); CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);//控制點1,終點 CGContextStrokePath(context); }
================================================================
以上引用
更多詳情
更多詳情