0 CGContextRef context = UIGraphicsGetCurrentContext(); 設置上下文
1 CGContextMoveToPoint 開始畫線
2 CGContextAddLineToPoint 畫直線
4 CGContextAddEllipseInRect 畫一橢圓
4 CGContextSetLineCap 設置線條終點形狀
4 CGContextSetLineDash 畫虛線
4 CGContextAddRect 畫一方框
4 CGContextStrokeRect 指定矩形
4 CGContextStrokeRectWithWidth 指定矩形線寬度
4 CGContextStrokeLineSegments 一些直線
5 CGContextAddArc 畫已曲線 前倆店爲中心 中間倆店爲起始弧度 最後一數據爲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,最後是奇數,那麼要被填充,偶數則不填充,和方向沒有關係。
Function
Description
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 混合倆種顏色
http://www.cocoachina.com/bbs/read.php?tid=75122&page=1
================================================================
IOS直線樣式
================================================================
虛線
畫虛線需要用到函數:
CGContextSetLineDash
此函數需要四個參數:
- context – 這個不用多說
- phase - 稍後再說
- lengths – 指明虛線是如何交替繪製,具體看例子
- count – lengths數組的長度
- 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個點,如此反覆,如圖:
如果把lengths值改爲{10, 20, 10},則表示先繪製10個點,跳過20個點,繪製10個點,跳過10個點,再繪製20個點,如此反覆,如圖:
注意count的值等於lengths數組的長度
phase參數表示在第一個虛線繪製的時候跳過多少個點,舉例說明:
- 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,如此反覆。
CGContextSetStrokeColorWithColor(myContext, [UIColor blackColor].CGColor);
CGContextSetLineDash (myContext,phase,lengths,2);
CGContextClosePath(myContext);
CGContextStrokePath(myContext);
================================================================
切線
- (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);
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);
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);
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);
CGContextStrokePath(context);
}
轉載自http://blog.csdn.net/zhenyu5211314/article/details/24230581?utm_source=tuicool