繪圖-UIBezierPath

UIBezierPath是在 UIKit 中的一個類,繼承于NSObject,可以創建基于矢量的路徑.此類是Core Graphics框架關于path的一個OC封裝。
所以 UIBezierPath 是基于 Core Graphics 實現的一項繪圖技術。

使用此類可以定義常見的圓形、多邊形等形狀 。我們使用直線、弧(arc)來創建復雜的曲線形狀。每一個直線段或者曲線段的結束的地方是下一個的開始的地方。每一個連接的直線或者曲線段的集合成為subpath。一個UIBezierPath對象定義一個完整的路徑包括一個或者多個subpaths。

下面我們看下, UIBezierPath類的頭文件里定義的方法有哪些:

UIBezierPath類頭文件定義

+ (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)CGPath NS_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.
// 設置線型
- (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 當前圖形上下文 中的路徑操作:
// 填充顏色
- (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;

值得注意的是:
UIBezierPath可以獨立繪圖,并不需要借助 CAShapeLayer等圖層。 使用UIBezierPath繪圖,必須要在一個UIView 的子類試圖中的drawRect:方法中實現。
不知道為什么的朋友可以移步到我的這篇文章:
UIView的layoutSubviews和drawRect


效果.png

具體的代碼實現的效果可以到我GitHub去下載查看,喜歡的話,請star 一下。

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

推薦閱讀更多精彩內容