簡(jiǎn)介:
? ? ? ? 貝塞爾曲線,是應(yīng)用于二維圖形應(yīng)用程序的數(shù)學(xué)曲線??梢詣?chuàng)建基于矢量的路徑。這個(gè)類在UIKit中。此類是Core Graphics框架對(duì)CGContextRef的一個(gè)封裝。使用此類可以定義簡(jiǎn)單的形狀,如橢圓或者矩形,或者有多個(gè)直線和曲線段組成的形狀。曲線定義:起始點(diǎn)、終止點(diǎn)(錨點(diǎn))、控制點(diǎn)。通過調(diào)整控制點(diǎn),貝塞爾曲線的形狀會(huì)發(fā)生變化。
使用UIBezierPath畫圖步驟:
1、創(chuàng)建一個(gè)UIBezierPath對(duì)象;
2、調(diào)用-moveToPoint:設(shè)置初始線段的起點(diǎn);
3、添加線或者曲線去定義一個(gè)或者多個(gè)子路徑;
4、改變UIBezierPath對(duì)象跟繪圖相關(guān)的屬性。如,我們可以設(shè)置畫筆的屬性、填充樣式等。
創(chuàng)建方法:
+ (instancetype)bezierPath;使用比較多,因?yàn)檫@方法創(chuàng)建的對(duì)象,我們可以根據(jù)我們的需要任意定制樣式,可以畫任何我們想畫的圖形。
+ (instancetype)bezierPathWithRect:(CGRect)rect;這方法根據(jù)一個(gè)矩形畫貝塞爾曲線。
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;這方法根據(jù)一個(gè)矩形畫內(nèi)切曲線。通常用它來畫圓或者橢圓。
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;畫矩形,但是這個(gè)矩形是可以畫圓角的。第一個(gè)參數(shù)是矩形,第二個(gè)參數(shù)是圓角大小。
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii; 畫矩形,但是可以指定某一個(gè)角畫成圓角。
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;用于畫圓弧,參數(shù)說明如下: center: 弧線中心點(diǎn)的坐標(biāo) radius: 弧線所在圓的半徑 startAngle: 弧線開始的角度值 endAngle: 弧線結(jié)束的角度值 clockwise: 是否順時(shí)針畫弧線。
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;這個(gè)方法創(chuàng)建并返回一個(gè) Core Graphics path。
- (UIBezierPath *)bezierPathByReversingPath;創(chuàng)建并返回一個(gè)新的反轉(zhuǎn)內(nèi)容的當(dāng)前路徑曲線路徑的對(duì)象。
構(gòu)建一個(gè)path:
- (void)moveToPoint:(CGPoint)point; 設(shè)置第一個(gè)起始點(diǎn)到接收器
- (void)addLineToPoint:(CGPoint)point;附加一條直線到接收器的路徑
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2; 該方法就是畫三次貝塞爾曲線的關(guān)鍵方法,以三個(gè)點(diǎn)畫一段曲線,一般和moveToPoint:配合使用。其實(shí)端點(diǎn)為moveToPoint:設(shè)置,終止端點(diǎn)位為endPoint;??刂泣c(diǎn)1的坐標(biāo)controlPoint1,這個(gè)參數(shù)可以調(diào)整??刂泣c(diǎn)2的坐標(biāo)是controlPoint2。
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;畫二次貝塞爾曲線,是通過調(diào)用此方法來實(shí)現(xiàn)的。一般和moveToPoint:配合使用。endPoint終端點(diǎn),controlPoint控制點(diǎn),對(duì)于二次貝塞爾曲線,只有一個(gè)控制點(diǎn)。
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise ;
- (void)closePath;閉合線
- (void)removeAllPoints;移除所有的點(diǎn)
- (void)appendPath:(UIBezierPath *)bezierPath;追加指定的路徑對(duì)象到內(nèi)容到接收器的路徑
- (void)applyTransform:(CGAffineTransform)transform;用指定的仿射變換矩陣變換路徑的所有點(diǎn)
path 信息:
@property(readonly,getter=isEmpty) BOOL empty;該值指示路徑是否有任何有效的元素。
@property(nonatomic,readonly) CGRect bounds;路徑包括的矩形。
@property(nonatomic,readonly) CGPoint currentPoint;圖形路徑中的當(dāng)前點(diǎn)。
- (BOOL)containsPoint:(CGPoint)point;接收器是否包含指定的點(diǎn)。
繪制屬性:
@property(nonatomic) CGFloat lineWidth; 線寬。
@property(nonatomic) CGLineCap lineCapStyle; 端點(diǎn)類型。
typedef CF_ENUM(int32_t, CGLineCap) {
? ? kCGLineCapButt, ? //默認(rèn)的
? ? kCGLineCapRound,//輕微圓角
? ? kCGLineCapSquare //正方形
};
@property(nonatomic) CGLineJoin lineJoinStyle; 連接類型。
typedef CF_ENUM(int32_t, CGLineJoin) {
? ? kCGLineJoinMiter, //默認(rèn)的表示斜接
? ? kCGLineJoinRound,//圓滑銜接
? ? kCGLineJoinBevel ? //斜角連接
};
@property(nonatomic) CGFloat miterLimit; 這個(gè)限制值有助于避免在連接線段志堅(jiān)的尖峰
@property(nonatomic) CGFloat flatness;確定彎曲路徑短的繪制精度的因素
@property(nonatomic) BOOL usesEvenOddFillRule; 判斷奇偶數(shù)組的規(guī)則繪制圖像路徑
- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase; 設(shè)置線型
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullable CGFloat *)phase; 檢索線型
當(dāng)前圖形上下文中的路徑操作:
- (void)fill;? 填充顏色
- (void)stroke; 各個(gè)點(diǎn)連線
- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;填充
- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
- (void)addClip;與封閉的接收器的路徑與當(dāng)前徒刑上下文的裁剪路徑的面積