UIBezierPath 貝塞爾路徑

使用UIBezierPath類可以創建基于矢量的路徑,這個類在UIKit中。

此類是Core Graphics框架關于path的一個封裝。

此類可以定義簡單的形狀,如橢圓或者矩形,或者有多個直線和曲線段組成的形狀。

Bezier Path 基礎

UIBezierPath對象是CGPathRef數據類型的封裝。path如果是基于矢量形狀的,都用直線和曲線段去創建。我們使用直線段去創建矩形和多邊形,使用曲線段去創建弧(arc),圓或者其他復雜的曲線形狀。每一段都包括一個或者多個點,繪圖命令定義如何去詮釋這些點。每一個直線段或者曲線段的結束的地方是下一個的開始的地方。每一個連接的直線或者曲線段的集合成為subpath。一個UIBezierPath對象定義一個完整的路徑包括一個或者多個subpaths。

UIBezierPath類頭文件定義

// 根據一個Rect畫一個矩形曲線

+ (instancetype)bezierPath;

*? 根據一個Rect畫一個橢圓曲線? Rect為正方形時畫的是一個圓

*? @param rect CGRect一個矩形

+ (instancetype)bezierPathWithRect:(CGRect)rect;

/**

*? 根據一個Rect畫一個圓角矩形曲線 (Radius:圓角半徑)? 當Rect為正方形時且Radius等于邊長一半時畫的是一個圓

*? @param rect CGRect一個矩形

*/

+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

/**

*? 根據一個Rect畫一個圓角矩形曲線? 當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

};

/**

*? 根據一個Rect針對四角中的某個或多個角設置圓角

*

*? @param rect? ? ? ? CGRect一個矩形

*? @param corners? ? 允許指定矩形的部分角為圓角,而其余的角為直角,取值來自枚舉

*? @param cornerRadii? 指定了圓角的半徑,這個參數的取值是 CGSize 類型,也就意味著這里需要給出的是橢圓的半徑。

*/

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

/**

*? 以某個中心點畫弧線

*? @param center? ? 指定了圓弧所在正圓的圓心點坐標

*? @param radius? ? 指定了圓弧所在正圓的半徑

*? @param startAngle 指定了起始弧度位置? 注意: 起始與結束這里是弧度

*? @param endAngle? 指定了結束弧度位置

*? @param clockwise? 指定了繪制方向,以時鐘方向為判斷基準? 看下圖

*/

+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;

圖片來自網絡

/**

*? 根據CGPath創建并返回一個新的UIBezierPath對象

*? @param CGPath CGPathRef

*/

+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

@property(nonatomic)CGPathRef CGPath;

- (CGPathRef)CGPathNS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;

/**

*? 設置第一個起始點到接收器

*? @param point 起點坐標

*/

- (void)moveToPoint:(CGPoint)point;

/**

*? 附加一條直線到接收器的路徑

*? @param point 要到達的坐標

*/

- (void)addLineToPoint:(CGPoint)point;

/**

*? 該方法就是畫三次貝塞爾曲線的關鍵方法,以三個點畫一段曲線,一般和moveToPoint:配合使用。其實端點為moveToPoint:設置,終止端點位為endPoint;。控制點1的坐標controlPoint1,這個參數可以調整。控制點2的坐標是controlPoint2。

*

*? @param endPoint? ? ? 終點坐標

*? @param controlPoint1 控制點1

*? @param controlPoint2 控制點2? 看下圖

*/

- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;

圖片來自網絡

/**

*? 畫二次貝塞爾曲線,是通過調用此方法來實現的。一般和moveToPoint:配合使用。endPoint終端點,controlPoint控制點,對于二次貝塞爾曲線,只有一個控制點

*? @param endPoint? ? 終點坐標

*? @param controlPoint 控制點? 看下圖

*/

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

圖片來自網絡

/**

*? 添加一個弧線 與bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:區別是bezierPathWithArcCenter它是初始化一個弧線,addArcWithCenter是添加一個弧線,共同點就是參數都一樣

*? @param center? ? 指定了圓弧所在正圓的圓心點坐標

*? @param radius? ? 指定了圓弧所在正圓的半徑

*? @param startAngle 指定了起始弧度位置

*? @param endAngle? 指定了結束弧度位置

*? @param clockwise? 指定了繪制方向,以時鐘方向為判斷基準

*/

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);

/**

*? 閉合線使用這個方法起始點與終點將相連

*/

- (void)closePath;

/**

*? 移除所有坐標點

*/

- (void)removeAllPoints;

// Appending paths

// 添加一個paths UIBezierPath

- (void)appendPath:(UIBezierPath *)bezierPath;

// Modified paths

// 創建并返回一個與當前路徑相反的新的貝塞爾路徑對象

- (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;

// 圖形路徑中的當前點

@property(nonatomic,readonly)CGPoint currentPoint;

// 接收器是否包含指定的點

- (BOOL)containsPoint:(CGPoint)point;

// Drawing properties

// 線寬

@property(nonatomic)CGFloat lineWidth;

typedef CF_ENUM(int32_t, CGLineCap) {

kCGLineCapButt,? 默認的

kCGLineCapRound, 輕微圓角

kCGLineCapSquare 正方形

};

// 端點類型

@property(nonatomic)CGLineCap lineCapStyle;

typedef CF_ENUM(int32_t, CGLineJoin) {

kCGLineJoinMiter, 默認的表示斜接

kCGLineJoinRound, 圓滑銜接

kCGLineJoinBevel? 斜角連接

};

// 連接類型

@property(nonatomic)CGLineJoin lineJoinStyle;

// 最大斜接長度? 斜接長度指的是在兩條線交匯處內角和外角之間的距離

@property(nonatomic)CGFloat miterLimit;// Used when lineJoinStyle is kCGLineJoinMiter

/*

最大斜接長度? 斜接長度指的是在兩條線交匯處內角和外角之間的距離

只有lineJoin屬性為kCALineJoinMiter時miterLimit才有效

邊角的角度越小,斜接長度就會越大。

為了避免斜接長度過長,我們可以使用 miterLimit屬性。

如果斜接長度超過 miterLimit的值,邊角會以 lineJoin的 "bevel"即kCALineJoinBevel類型來顯示

*/

// 確定彎曲路徑短的繪制精度的因素

@property(nonatomic)CGFloat flatness;

// 一個bool值指定even-odd規則是否在path可用

@property(nonatomic)BOOL usesEvenOddFillRule;// Default is NO. When YES, the even-odd fill rule is used for drawing, clipping, and hit testing.

// 設置線型 可設置成虛線

CGFloat pattern[] = {1,1};

- (void)setLineDash:(nullableconst CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;

//? 檢索線型

- (void)getLineDash:(nullableCGFloat *)pattern count:(nullableNSInteger *)count phase:(nullableCGFloat *)phase;

// Path operations on the current graphics context 當前圖形上下文中的路徑操作:

// 填充顏色

- (void)fill;

// 利用當前繪圖屬性沿著接收器的路徑繪制

- (void)stroke;

// These methods do not affect the blend mode or alpha of the current graphics context

// 用指定的混合模式和透明度值來描繪受接收路徑所包圍的區域

- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

// 使用指定的混合模式和透明度值沿著接收器路徑。繪制一行

- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

// 剪切被接收者路徑包圍的區域該路徑是帶有剪切路徑的當前繪圖上下文。使得其成為我們當前的剪切路徑

- (void)addClip;

*/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容