UIBezierPath是在 UIKit 中的一個類墩蔓,繼承于NSObject,可以創(chuàng)建基于矢量的路徑.此類是Core Graphics框架關(guān)于path的一個OC封裝坛梁。
所以 UIBezierPath 是基于 Core Graphics 實現(xiàn)的一項繪圖技術(shù)。
使用此類可以定義常見的圓形颤专、多邊形等形狀 孽锥。我們使用直線概行、弧(arc)來創(chuàng)建復(fù)雜的曲線形狀儒鹿。每一個直線段或者曲線段的結(jié)束的地方是下一個的開始的地方化撕。每一個連接的直線或者曲線段的集合成為subpath。一個UIBezierPath對象定義一個完整的路徑包括一個或者多個subpaths约炎。
下面我們看下侯谁, UIBezierPath類的頭文件里定義的方法有哪些:
UIBezierPath類頭文件定義
+ (instancetype)bezierPath;
/**
* 根據(jù)一個Rect 畫一個橢圓曲線 Rect為正方形時 畫的是一個圓
* @param rect CGRect一個矩形
*/
+ (instancetype)bezierPathWithRect:(CGRect)rect;
/**
* 根據(jù)一個Rect 畫一個圓角矩形曲線 (Radius:圓角半徑) 當(dāng)Rect為正方形時且Radius等于邊長一半時 畫的是一個圓
* @param rect CGRect一個矩形
*/
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
/**
* 根據(jù)一個Rect 畫一個圓角矩形曲線 當(dāng)Rect為正方形時且Radius等于 邊長一半時 畫的是一個圓
* @param rect CGRect一個矩形
* @param cornerRadius 圓角半徑
*/
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
/**
* 根據(jù)一個Rect 針對四角中的某個或多個角設(shè)置圓角
*
* @param rect CGRect一個矩形
* @param corners 允許指定矩形的部分角為圓角,而其余的角為直角,取值來自枚舉
* @param cornerRadii 指定了圓角的半徑墙贱,這個參數(shù)的取值是 CGSize 類型热芹,也就意味著這里需要給出的是橢圓的半徑。
*/
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
/**
* 以某個中心點畫弧線
* @param center 指定了圓弧所在正圓的圓心點坐標(biāo)
* @param radius 指定了圓弧所在正圓的半徑
* @param startAngle 指定了起始弧度位置 注意: 起始與結(jié)束這里是弧度
* @param endAngle 指定了結(jié)束弧度位置
* @param clockwise 指定了繪制方向惨撇,以時鐘方向為判斷基準(zhǔn) 看下圖
*/
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
圖片來自網(wǎng)絡(luò)
/**
* 根據(jù)CGPath創(chuàng)建并返回一個新的UIBezierPath對象
* @param CGPath CGPathRef
*/
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
@property(nonatomic) CGPathRef CGPath;
- (CGPathRef)CGPath NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;
/**
* 設(shè)置第一個起始點到接收器
* @param point 起點坐標(biāo)
*/
- (void)moveToPoint:(CGPoint)point;
/**
* 附加一條直線到接收器的路徑
* @param point 要到達(dá)的坐標(biāo)
*/
- (void)addLineToPoint:(CGPoint)point;
/**
* 該方法就是畫三次貝塞爾曲線的關(guān)鍵方法伊脓,以三個點畫一段曲線,一般和moveToPoint:配合使用魁衙。其實端點為moveToPoint:設(shè)置报腔,終止端點位為endPoint;剖淀〈慷辏控制點1的坐標(biāo)controlPoint1,這個參數(shù)可以調(diào)整纵隔》撸控制點2的坐標(biāo)是controlPoint2。
*
* @param endPoint 終點坐標(biāo)
* @param controlPoint1 控制點1
* @param controlPoint2 控制點2 看下圖
*/
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
圖片來自網(wǎng)絡(luò)
/**
* 畫二次貝塞爾曲線捌刮,是通過調(diào)用此方法來實現(xiàn)的碰煌。一般和moveToPoint:配合使用。endPoint終端點绅作,controlPoint控制點芦圾,對于二次貝塞爾曲線,只有一個控制點
* @param endPoint 終點坐標(biāo)
* @param controlPoint 控制點 看下圖
*/
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
圖片來自網(wǎng)絡(luò)
/**
* 添加一個弧線 與 bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:區(qū)別是bezierPathWithArcCenter它是初始化一個弧線,addArcWithCenter是添加一個弧線,共同點就是參數(shù)都一樣
* @param center 指定了圓弧所在正圓的圓心點坐標(biāo)
* @param radius 指定了圓弧所在正圓的半徑
* @param startAngle 指定了起始弧度位置
* @param endAngle 指定了結(jié)束弧度位置
* @param clockwise 指定了繪制方向俄认,以時鐘方向為判斷基準(zhǔn)
*/
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);
/**
* 閉合線 使用這個方法起始點與終點將相連
*/
- (void)closePath;
/**
* 移除所有坐標(biāo)點
*/
- (void)removeAllPoints;
// Appending paths
// 添加一個paths UIBezierPath
- (void)appendPath:(UIBezierPath *)bezierPath;
// Modified paths
// 創(chuàng)建 并返回一個與當(dāng)前路徑相反的新的貝塞爾路徑對象
- (UIBezierPath *)bezierPathByReversingPath NS_AVAILABLE_IOS(6_0);
// Transforming paths
// 用指定的仿射變換矩陣變換路徑的所有點
- (void)applyTransform:(CGAffineTransform)transform;
// Path info
// 該值指示路徑是否有任何有效的元素个少。
@property(readonly,getter=isEmpty) BOOL empty;
// 路徑包括的矩形
@property(nonatomic,readonly) CGRect bounds;
// 圖形路徑中的當(dāng)前點
@property(nonatomic,readonly) CGPoint currentPoint;
// 接收器是否包含指定的點
- (BOOL)containsPoint:(CGPoint)point;
// Drawing properties
// 線寬
@property(nonatomic) CGFloat lineWidth;
typedef CF_ENUM(int32_t, CGLineCap) {
kCGLineCapButt, 默認(rèn)的
kCGLineCapRound, 輕微圓角
kCGLineCapSquare 正方形
};
// 端點類型
@property(nonatomic) CGLineCap lineCapStyle;
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter, 默認(rèn)的表示斜接
kCGLineJoinRound, 圓滑銜接
kCGLineJoinBevel 斜角連接
};
// 連接類型
@property(nonatomic) CGLineJoin lineJoinStyle;
// 最大斜接長度 斜接長度指的是在兩條線交匯處內(nèi)角和外角之間的距離
@property(nonatomic) CGFloat miterLimit; // Used when lineJoinStyle is kCGLineJoinMiter
/*
最大斜接長度 斜接長度指的是在兩條線交匯處內(nèi)角和外角之間的距離
只有l(wèi)ineJoin屬性為kCALineJoinMiter時miterLimit才有效
邊角的角度越小,斜接長度就會越大眯杏。
為了避免斜接長度過長稍算,我們可以使用 miterLimit 屬性。
如果斜接長度超過 miterLimit 的值役拴,邊角會以 lineJoin的 "bevel"即kCALineJoinBevel類型來顯示
*/
// 確定彎曲路徑短的繪制精度的因素
@property(nonatomic) CGFloat flatness;
// 一個bool值 指定even-odd規(guī)則是否在path可用
@property(nonatomic) BOOL usesEvenOddFillRule; // Default is NO. When YES, the even-odd fill rule is used for drawing, clipping, and hit testing.
// 設(shè)置線型
- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;
// 檢索線型
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullableCGFloat *)phase;
// Path operations on the current graphics context 當(dāng)前圖形上下文 中的路徑操作:
// 填充顏色
- (void)fill;
// 利用當(dāng)前繪圖屬性沿著接收器的路徑繪制
- (void)stroke;
// These methods do not affect the blend mode or alpha of the current graphics context
// 用指定的混合模式和透明度值來描繪受接收路徑所包圍的區(qū)域
- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
// 使用指定的混合模式和透明度值沿著接收器路徑糊探。繪制一行
- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
// 剪切被接收者路徑包圍的區(qū)域 該路徑是帶有剪切路徑的當(dāng)前繪圖上下文。使得其成為我們當(dāng)前的剪切路徑
- (void)addClip;
值得注意的是:
UIBezierPath可以獨立繪圖河闰,并不需要借助 CAShapeLayer等圖層科平。 使用UIBezierPath繪圖,必須要在一個UIView 的子類試圖中的drawRect:方法中實現(xiàn)姜性。
不知道為什么的朋友可以移步到我的這篇文章:
UIView的layoutSubviews和drawRect
效果.png
具體的代碼實現(xiàn)的效果可以到我GitHub去下載查看瞪慧,喜歡的話,請star 一下部念。