獨(dú)立CGPath總結(jié)
CGPathCreateMutable //創(chuàng)建一個(gè)可變圖形的路徑 需要自己釋放
CGPathCreateWithEllipseInRect //創(chuàng)建一個(gè)橢圓形的不可改變的路徑
CGPathCreateWithRect //創(chuàng)建一個(gè)不變的矩形路徑
CGPathCreateCopy //創(chuàng)建一個(gè)不可變的可以拷貝的路徑
CGPathCreateCopyByDashingPath //創(chuàng)建一個(gè)虛線(xiàn)路徑可以復(fù)制的
CGPathCreateCopyByStrokingPath //創(chuàng)建一個(gè)畫(huà)的路徑
CGPathCreateMutableCopy //創(chuàng)建一個(gè)現(xiàn)有的圖形路徑的副本
//由一個(gè)轉(zhuǎn)換矩陣變換一個(gè)圖形路徑創(chuàng)建一個(gè)不可變的副本
CGPathCreateCopyByTransformingPath
//創(chuàng)建一個(gè)由一個(gè)轉(zhuǎn)換矩陣變換一個(gè)圖形路徑的可變副本
CGPathCreateMutableCopyByTransformingPath
CGPathRelease //遞減保留的圖形路徑計(jì)數(shù)
CGPathRetain //增加保留的圖形路徑計(jì)數(shù)
CGPathAddArc 一個(gè)//弧形追加一個(gè)可變圖形的路徑, 可能由直線(xiàn)段之前
CGPathAddEllipseInRect //添加一個(gè)適合的橢圓在矩形的內(nèi)部
CGPathEqualToPath //兩個(gè)圖形路徑是否相等
CGPathGetBoundingBox //返回圖形路徑中包含所有點(diǎn)的包圍盒
CGPathGetPathBoundingBox //返回圖形路徑的邊界框
CGPathGetCurrentPoint //返回當(dāng)前點(diǎn)的路徑
CGPathGetTypeID //返回Quartz圖形路徑的核心基礎(chǔ)類(lèi)型的標(biāo)識(shí)符
CGPathIsEmpty //指示路徑是否為空
CGPathIsRect //圖形路徑指示是否代表一個(gè)矩形
CGPathContainsPoint //檢查一個(gè)點(diǎn)是否在圖形路徑中
與CGContext關(guān)聯(lián)的Path總結(jié)
點(diǎn)&線(xiàn)
注意
- AddLineToPoint實(shí)現(xiàn)方式在線(xiàn)條alpha為1,即不透明的時(shí)候和AddLines一樣,而且是實(shí)時(shí)畫(huà)線(xiàn)。
- 但是當(dāng)線(xiàn)條半透明的時(shí)候,AddLines在一條線(xiàn)自身重疊時(shí)不會(huì)透明度重疊。
- 而AddLineToPoint卻會(huì)導(dǎo)致透明度重疊,且move touch的點(diǎn)出也會(huì)出現(xiàn)透明度重疊,會(huì)顯示成點(diǎn)和點(diǎn)之間透明度正確,點(diǎn)上不透明的問(wèn)題。
//獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//點(diǎn)(在Context中移動(dòng)畫(huà)筆到(0,0)個(gè)點(diǎn))
CGContextMoveToPoint(context, 0, 0);
//線(xiàn)(畫(huà)筆從上一個(gè)點(diǎn)畫(huà)到(1,1)這個(gè)點(diǎn))
CGContextAddLineToPoint(context, 1, 1);
//添加點(diǎn)的坐標(biāo)
CGPoint points[] = {
CGPointMake(0, 0),
CGPointMake(2, 2),
CGPointMake(3, 3)
};
int count = sizeof(points)/sizeof(points[0]);//求出poins長(zhǎng)度
CGContextAddLines(context, points, count);//畫(huà)線(xiàn)
/*注意
AddLineToPoint實(shí)現(xiàn)方式在線(xiàn)條alpha為1,即不透明的時(shí)候和AddLines一樣,而且是實(shí)時(shí)畫(huà)線(xiàn)。
但是當(dāng)線(xiàn)條半透明的時(shí)候,AddLines在一條線(xiàn)自身重疊時(shí)不會(huì)透明度重疊。
而AddLineToPoint卻會(huì)導(dǎo)致透明度重疊,且move touch的點(diǎn)出也會(huì)出現(xiàn)透明度重疊,會(huì)顯示成點(diǎn)和點(diǎn)之間透明度正確,點(diǎn)上不透明的問(wèn)題。
*/
曲線(xiàn)
一種
void CGContextAddArc (
CGContextRef c,
CGFloat x, //圓心的x坐標(biāo)
CGFloat y, //圓心的x坐標(biāo)
CGFloat radius, //圓的半徑
CGFloat startAngle, //開(kāi)始弧度
CGFloat endAngle, //結(jié)束弧度
int clockwise //0表示順時(shí)針,1表示逆時(shí)針
);
假如想創(chuàng)建一個(gè)完整的圓圈,那么 開(kāi)始弧度就是0 結(jié)束弧度是 2pi, 因?yàn)閳A周長(zhǎng)是 2*pi*r.
最后,函數(shù)執(zhí)行完后,current point就被重置為(x,y).
還有一點(diǎn)要注意的是,假如當(dāng)前path已經(jīng)存在一個(gè)subpath,那么這個(gè)函數(shù)執(zhí)行的另外一個(gè)效果是
會(huì)有一條直線(xiàn),從current point到弧的起點(diǎn)
第二種
void CGContextAddArcToPoint (
CGContextRef c,
CGFloat x1, //端點(diǎn)1的x坐標(biāo)
CGFloat y1, //端點(diǎn)1的y坐標(biāo)
CGFloat x2, //端點(diǎn)2的x坐標(biāo)
CGFloat y2, //端點(diǎn)2的y坐標(biāo)
CGFloat radius //半徑
);
原理:首先畫(huà)兩條線(xiàn),這兩條線(xiàn)分別是 current point to (x1,y1) 和(x1,y1) to (x2,y2).
這樣就是出現(xiàn)一個(gè)以(x1,y1)為頂點(diǎn)的兩條射線(xiàn),
然后定義半徑長(zhǎng)度,這個(gè)半徑是垂直于兩條射線(xiàn)的,這樣就能決定一個(gè)圓了,更好的理解看下圖,不過(guò)個(gè)人認(rèn)為下圖所標(biāo)的 tangent point 1的位置是錯(cuò)誤的。
最后,函數(shù)執(zhí)行完后,current point就被重置為(x2,y2).
還有一點(diǎn)要注意的是,假如當(dāng)前path已經(jīng)存在一個(gè)subpath,那么這個(gè)函數(shù)執(zhí)行的另外一個(gè)效果是
會(huì)有一條直線(xiàn),從current point到(x1,y1)
Paths - happy dog - 又一個(gè)部落格
傳說(shuō)中的不規(guī)則的曲線(xiàn)
畫(huà)曲線(xiàn),一般是一條直線(xiàn),然后定義幾個(gè)控制點(diǎn),使直線(xiàn)變彎曲。
三次曲線(xiàn)函數(shù)
void CGContextAddCurveToPoint (
CGContextRef c,
CGFloat cp1x, //控制點(diǎn)1 x坐標(biāo)
CGFloat cp1y, //控制點(diǎn)1 y坐標(biāo)
CGFloat cp2x, //控制點(diǎn)2 x坐標(biāo)
CGFloat cp2y, //控制點(diǎn)2 y坐標(biāo)
CGFloat x, //直線(xiàn)的終點(diǎn) x坐標(biāo)
CGFloat y //直線(xiàn)的終點(diǎn) y坐標(biāo)
);
假如第二個(gè)控制點(diǎn)(cp2x,cp2y)比(cp1x,cp1y) 更接近c(diǎn)urrent point,那么會(huì)形成一個(gè)封閉的曲線(xiàn)
Paths - happy dog - 又一個(gè)部落格
二次曲線(xiàn)函數(shù)
void CGContextAddQuadCurveToPoint (
CGContextRef c,
CGFloat cpx, //控制點(diǎn) x坐標(biāo)
CGFloat cpy, //控制點(diǎn) y坐標(biāo)
CGFloat x, //直線(xiàn)的終點(diǎn) x坐標(biāo)
CGFloat y //直線(xiàn)的終點(diǎn) y坐標(biāo)
);
裁剪的方法:
CGConextClip;
CGContextEOClip;
CGContextClipToRect;
CGContextClipToRects;
CGContextClipToMask;
圖像繪制
UIGraphicsBeginImageContextWithOptions
UIGraphicsEndImageContext
PDF繪制
UIGraphicsBeginPDFContextToFile(ToData)
UIGraphicsEndPDFContext
翻轉(zhuǎn)屏幕變換:
CGContextTranslateCTM(graphicsContext, 0.0, drawingRect.size.height);
CGContextScaleCTM(graphicsContext, 1.0, -1.0);
** CGPATH &CGCONTEXT相關(guān)聯(lián)的CGPath中的對(duì)應(yīng)關(guān)系**
CGPathCreateMutable = CGContextBeginPath//創(chuàng)建一個(gè)路徑
CGPathMoveToPoint = CGContextMoveToPoint//畫(huà)筆(路徑)從哪里開(kāi)始
CGPathAddLineToPoint = CGContextAddLineToPoint//添加一個(gè)點(diǎn)(線(xiàn))
CGPathAddCurveToPoint = CGContextAddCurveToPoint//創(chuàng)建一個(gè)曲線(xiàn)
CGPathAddEllipseInRect = CGContextAddEllipseInRect//創(chuàng)建一個(gè)橢圓
CGPathAddArc = CGContextAddArc//創(chuàng)建一個(gè)圓形路徑
CGPathAddRect = CGContextAddRect//創(chuàng)建一個(gè)矩形路徑
CGPathCloseSubPath = CGContextCloseSubPath//閉合路徑
UIBezierPath
好了看完了上面的各種函數(shù)是不是頭都大了那么看看我們的UIBezierPath
屬性
CGPath:將UIBezierPath類(lèi)轉(zhuǎn)換成CGPath,類(lèi)似于UIColor的CGColor
empty:只讀類(lèi)型,路徑上是否有有效的元素
bounds:和view的bounds是不一樣的,它獲取path的X坐標(biāo)、Y坐標(biāo)、寬度,但是高度為0
currentPoint:當(dāng)前path的位置,可以理解為path的終點(diǎn)
lineWidth:path寬度
lineCapStyle:path端點(diǎn)樣式,有3種樣式
kCGLineCapButt:無(wú)端點(diǎn)
kCGLineCapRound:圓形端點(diǎn)
kCGLineCapSquare:方形端點(diǎn)(樣式上和kCGLineCapButt是一樣的,但是比kCGLineCapButt長(zhǎng)一點(diǎn))
lineJoinStyle:拐角樣式
kCGLineJoinMiter:尖角
kCGLineJoinRound:圓角
kCGLineJoinBevel:缺角
miterLimit:最大斜接長(zhǎng)度(只有在使用kCGLineJoinMiter是才有效), 邊角的角度越小,斜接長(zhǎng)度就會(huì)越大
為了避免斜接長(zhǎng)度過(guò)長(zhǎng),使用lineLimit屬性限制,如果斜接長(zhǎng)度超過(guò)miterLimit,邊角就會(huì)以KCALineJoinBevel類(lèi)型來(lái)顯示
flatness:彎曲路徑的渲染精度,默認(rèn)為0.6,越小精度越高,相應(yīng)的更加消耗性能。
usesEvenOddFillRule:?jiǎn)坞p數(shù)圈規(guī)則是否用于繪制路徑,默認(rèn)是NO
UIRectCorner:角
UIRectCornerTopLeft:左上角
UIRectCornerTopRight:右上角
UIRectCornerBottomLeft:左下角
UIRectCornerBottomRight:右下角
UIRectCornerAllCorners:所有四個(gè)角
UIBezierPath
1.創(chuàng)建UIBezierPath
//矩形路徑(rect矩形的位置和大小)
-(instancetype)bezierPathWithRect:(CGRect)rect:
//創(chuàng)建在rect里的內(nèi)切曲線(xiàn): (參數(shù):rect-矩形的Frame)
+(instancetype)bezierPathWithOvalInRect:(CGRect)rect:
//創(chuàng)建帶有圓角的矩形,當(dāng)矩形變成正圓的時(shí)候,Radius就不再起作用:
//rect:矩形的Frame cornerRadius:圓角大小)
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
//圓角的矩形(設(shè)定特定的角為)
/*
rect:矩形的Frame
corners:指定的圓角
cornerRadii:圓角的大小
*/
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect
byRoundingCorners:(UIRectCorner)corners
cornerRadii:(CGSize)cornerRadii;
//圓弧
/*
center->圓點(diǎn)
radius->半徑
startAngle->起始位置
endAngle->結(jié)束為止
clockwise->是否順時(shí)針?lè)较?*/
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise;
//通過(guò)已有路徑創(chuàng)建路徑:
//CGPath->已有路徑
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath
一些對(duì)象方法
-(void)moveToPoint:(CGPoint)point; //移動(dòng)到某一點(diǎn):(point:目標(biāo)位置)
-(void)addLineToPoint:(CGPoint)point; //繪制一條線(xiàn):(point:目標(biāo)位置)
- (void)closePath;//閉合路徑,即在終點(diǎn)和起點(diǎn)連一根線(xiàn)
-(void)appendPath:(UIBezierPath *)bezierPath;//追加路徑
-(UIBezierPath *)bezierPathByReversingPath;//扭轉(zhuǎn)路徑,即起點(diǎn)變成終點(diǎn),終點(diǎn)變成起點(diǎn)
-(void)applyTransform:(CGAffineTransform)transform;//路徑進(jìn)行仿射變換
-(void)fill; //填充;
-(void)stroke; //描邊,路徑創(chuàng)建需要描邊才能顯示出來(lái);
-(void)setStroke;//設(shè)置描邊顏色,需要在設(shè)置后調(diào)用描邊方法:
-(void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha; //設(shè)置描邊的混合模式
//設(shè)置描邊的混合模式
//blendMode->混合模式 alpha->透明度
-(void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
//設(shè)置填充的混合模式:
//blendMode:混合模式 alpha:透明度
-(void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
//修改當(dāng)前圖形上下文的繪圖區(qū)域可見(jiàn),隨后的繪圖操作導(dǎo)致呈現(xiàn)內(nèi)容只有發(fā)生在指定路徑的填充區(qū)域
-(void)addClip;
/**
* 創(chuàng)建虛線(xiàn)
* @param pattern C類(lèi)型線(xiàn)性數(shù)據(jù)
* @param count pattern中數(shù)據(jù)個(gè)數(shù)
* @param phase 起始位置
*/
- (void)setLineDash:(nullable const CGFloat *)pattern
count:(NSInteger)count
phase:(CGFloat)phase;
//清空路徑:
-(void)removeAllPoints;
/**
* 創(chuàng)建三次貝塞爾曲線(xiàn)
* @param endPoint 終點(diǎn)
* @param controlPoint1 控制點(diǎn)1
* @param controlPoint2 控制點(diǎn)2
*/
-(void)addCurveToPoint:(CGPoint)endPoint
controlPoint1:(CGPoint)controlPoint1
controlPoint2:(CGPoint)controlPoint2;
/**
* 創(chuàng)建二次貝塞爾曲線(xiàn)
* @param endPoint 終點(diǎn)
* @param controlPoint 控制點(diǎn)
*/
-(void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
/**
* 創(chuàng)建圓弧
* @param center 圓點(diǎn)
* @param radius 半徑
* @param startAngle 起始位置
* @param endAngle 結(jié)束為止
* @param clockwise 是否順時(shí)針?lè)较? */
- (void)addArcWithCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise;