以前花過一些時間看了CoreAnimation
的一些東西,在使用CoreAnimation
的時候,知道繪制路徑要使用UIBezierPath
,顏色要設置成CGColor
,圖片要設置成CGImage
。為什么不設置成UIColor
,UIImage
而要設置成CGColor
,CGImage
呢,不知道~只知道就這么設置就對了。知其然,卻不知其所以然。后來通過搜索知道了CoreGraphics
這么個框架,明白了CGColor
,CGImage
都是這個框架里面的內容。CoreAnimation
是一個跨平臺的動畫框架,它里面的類都是以CA開頭的;所以,沒錯,CoreGraphics
是一個跨平臺的圖形框架,它的類都是CG開頭的,CGRect
,CGPoint
等都是它里面的內容。
UIKit
是一個iOS的專有框架,我們通常使用的UIButton
,UILabel
等控件都是UIKit
里邊的內容,而UIKit
是基于CoreGraphics
實現的,也就是控件的顯示都是通過CoreGraphics
完成的,UIKit
是對它的一個封裝,所以對圖形顯示功能有更高要求的話,還是推薦使用CoreGraphics
。
繪制一般圖形文字:獲取圖形上下文要在drawRect:
方法中使用UIGraphicsGetCurrentContext()
獲取,這方法也只能在drawRect:
方法中才能獲取上下文。
使用CoreGraphics
繪制一個不封閉的矩形:
//1.獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.繪制路徑
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 100, 100);
CGPathAddLineToPoint(path, nil, 100, 200);
CGPathAddLineToPoint(path, nil, 300, 200);
CGPathAddLineToPoint(path, nil, 300, 100);
//3.添加路徑
CGContextAddPath(context, path);
//4.設置顯示屬性
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1); //路徑顏色
CGContextSetRGBFillColor(context, 0, 0, 1.0, 1); //設置填充色
CGContextSetLineCap(context, kCGLineCapRound); //路徑端點樣式
CGContextSetLineJoin(context, kCGLineJoinRound); //路徑拐點樣式
CGContextSetLineWidth(context, 5); //路徑寬度
//5.繪制圖形
/** CGPathDrawingMode填充模式
* kCGPathFill 不繪制邊框,只繪制填充
* kCGPathEOFill 奇偶規則繪制填充
* kCGPathStroke 不繪制填充,只繪制邊框
* kCGPathFillStroke 繪制邊框和填充
* kCGPathEOFillStroke 奇偶規則繪制邊框和填充
*/
CGContextDrawPath(context, kCGPathFillStroke);
//6.釋放路徑
CGPathRelease(path);
創建添加上下文內容可以簡化,上面的圖形的代碼簡化后為:
CGContextRef context1 = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context1, 100, 100);
CGContextAddLineToPoint(context1, 100, 200);
CGContextAddLineToPoint(context1, 300, 200);
CGContextAddLineToPoint(context1, 300, 100);
CGContextSetLineWidth(context1, 5);
CGContextSetLineCap(context1, kCGLineCapRound);
CGContextSetLineJoin(context1, kCGLineJoinRound);
[[UIColor redColor] setStroke];
[[UIColor blueColor] setFill];
CGContextDrawPath(context1, kCGPathFillStroke);
繪制一個矩形
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect squareRect = CGRectMake(100, 100, 200, 80);
CGContextAddRect(context, squareRect);
[[UIColor redColor] setStroke];
[[UIColor blueColor] setFill];
CGContextSetLineWidth(context, 2);
CGContextDrawPath(context, kCGPathFillStroke);
繪制一個橢圓
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect ellipseRect = CGRectMake(100, 100, 200, 80);
CGContextAddEllipseInRect(context, ellipseRect);
[[UIColor blueColor] setFill];
CGContextDrawPath(context, kCGPathFill);
繪制一個弧形
CGContextRef context = UIGraphicsGetCurrentContext();
/*
x:弧心x坐標
y:弧心y坐標
radius:半徑
startAngle:圓弧起始角度
endAngle:圓弧終止角度
clockwise:1為逆時針繪制,0為順時針繪制
*/
CGContextAddArc(context, [UIScreen mainScreen].bounds.size.width/2, 200, 80, -M_PI, 0, 0);
[[UIColor redColor] setStroke];
[[UIColor yellowColor] setFill];
CGContextSetLineWidth(context, 5);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextDrawPath(context, kCGPathFillStroke);
繪制貝塞爾曲線
CGContextRef context = UIGraphicsGetCurrentContext();
//二次貝塞爾曲線
CGContextMoveToPoint(context, 100, 100);
/*
cpx:控制點x坐標
cpy:控制點y坐標
x:結束點x坐標
y:結束點y坐標
*/
CGContextAddQuadCurveToPoint(context, 150, 0, 300, 100);
//三次貝塞爾曲線
CGContextMoveToPoint(context, 100, 200);
/*
cp1x:第一個控制點x坐標
cp1y:第一個控制點y坐標
cp2x:第二個控制點x坐標
cp2y:第二個控制點y坐標
x:結束點x坐標
y:結束點y坐標
*/
CGContextAddCurveToPoint(context, 150, 100, 250, 300, 300, 200);
[[UIColor redColor] setStroke];
CGContextSetLineWidth(context, 3);
CGContextDrawPath(context, kCGPathStroke);
繪制文字
NSString *contextStr = @"You don't know about real loss……'cause that only occurs when you love something more than you love yourself. I doubt you've ever dared to love anybody that much. ";
CGRect textRect = CGRectMake(80, 80, [UIScreen mainScreen].bounds.size.width-160, 120);
UIFont *textFont = [UIFont systemFontOfSize:15];
UIColor *textColor = [UIColor orangeColor];
[contextStr drawInRect:textRect withAttributes:@{NSFontAttributeName:textFont,NSForegroundColorAttributeName:textColor}];
繪制圖片
UIImage *img = [UIImage imageNamed:@"ss(4)"];
//從一點開始繪制,繪制的尺寸就是圖片的尺寸
[img drawAtPoint:CGPointMake(0, 20)];
//繪制在指定區域,圖片會拉伸
[img drawInRect:CGRectMake(0, 280, [UIScreen mainScreen].bounds.size.width, 100)];
//平鋪繪制
[img drawAsPatternInRect:CGRectMake(0, 430, [UIScreen mainScreen].bounds.size.width/2-5, 80)];
[img drawAsPatternInRect:CGRectMake([UIScreen mainScreen].bounds.size.width/2+5, 430, [UIScreen mainScreen].bounds.size.width/2, 80)];
上下文變換
CGContextRef context = UIGraphicsGetCurrentContext();
//平移
CGContextTranslateCTM(context, 100, 50);
//縮放
CGContextScaleCTM(context, 0.7, 0.7);
//旋轉
CGContextRotateCTM(context, M_PI_4/2);
UIImage *img = [UIImage imageNamed:@"ss(4)"];
[img drawInRect:CGRectMake(0, 0, 300, 160)];
繪制顏色漸變的圖層
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat compoents[12]={
248.0/255.0,86.0/255.0,86.0/255.0,1,
249.0/255.0,127.0/255.0,127.0/255.0,1,
1.0,1.0,1.0,1
};
CGFloat locations[3]={0,0.3,1.0};
/*
space:顏色空間
components:顏色數組,RGB顏色空間下,每四個數表示一種顏色(red,green,blue,alpha)
locations:每一種顏色的位置
count:漸變個數,和locations個數相同
*/
CGGradientRef gradient= CGGradientCreateWithColorComponents(colorSpace, compoents, locations, 3);
//線性漸變
/*
gradient:漸變色
startPoint:起始點
endPoint:終點
options:繪制方式(kCGGradientDrawsBeforeStartLocation:起始點之前就繪制,終點之后不再繪制
kCGGradientDrawsAfterEndLocation:起始點之前不進行繪制,終點之后繼續繪制)
*/
CGContextDrawLinearGradient(context, gradient, CGPointMake(50, 0), CGPointMake(300, 0), kCGGradientDrawsAfterEndLocation);
// //徑向漸變
// /*
// gradient:漸變色
// startCenter:起始點
// startRadius:起始點半徑
// endCenter:結束點
// endRadius:結束點半徑
// options:繪制方式(kCGGradientDrawsBeforeStartLocation:起始點之前就繪制,終點之后不再繪制
// kCGGradientDrawsAfterEndLocation:起始點之前不進行繪制,終點之后繼續繪制)
// */
// CGContextDrawRadialGradient(context, gradient, CGPointMake(200, 200),100, CGPointMake(200, 200), 150, kCGGradientDrawsBeforeStartLocation);
CGColorSpaceRelease(colorSpace);
參考鏈接:
https://developer.apple.com/documentation/coregraphics
http://www.cnblogs.com/kenshincui/p/3959951.html