UIBezierPath(貝塞爾曲線)學習記錄

原文地址
UIBezierPath類可以創(chuàng)建基于矢量的路徑,這個類在UIKit中。此類是Core Graphics框架關(guān)于path的一個封裝。使用此類可以定義簡單的形狀,如橢圓或者矩形,或者有多個直線和曲線段組成的形狀。

// 根據(jù)Rect 畫一個矩形曲線
+ (instancetype)bezierPathWithRect:(CGRect)rect;

// 根據(jù)Rect 畫一個橢圓曲線  Rect為正方形時 畫的是一個圓
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

// 根據(jù)Rect 畫一個圓角矩形曲線 (Radius:圓角半徑) Rect為正方形時且Radius等于邊長一半時 畫的是一個圓
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

// 根據(jù)Rect 針對四角中的某個或多個角設(shè)置圓角 corners:枚舉 矩形的部分角為圓角 cornerRadii:CGSize  圓角的半徑
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSiz)cornerRadii;


// 以某個中心點畫弧線  center:圓心點坐標 radius:圓的半徑 startAngle::起始弧度位置 endAngle:結(jié)束弧度位置   clockwise:是否為順時針方向
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;

 // 根據(jù)CGPath創(chuàng)建并返回一個新的UIBezierPath對象
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

 // 設(shè)置第一個起始點
- (void)moveToPoint:(CGPoint)point;

// 添加一條直線 point:要到達的坐標
- (void)addLineToPoint:(CGPoint)point;
// 該方法就是畫三次貝塞爾曲線的關(guān)鍵方法,以三個點畫一段曲線,一般和moveToPoint:配合使用。其實端點為moveToPoint:設(shè)置,終點位為endPoint 
// 控制點1的坐標controlPoint1,這個參數(shù)可以調(diào)整??刂泣c2的坐標是controlPoint2。 如下圖
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
// 二次貝塞爾曲線 一般和moveToPoint:配合使用。endPoint終點,controlPoint控制點 如下圖
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
// 添加一個弧線 與bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:區(qū)別是bezierPathWithArcCenter它是初始化一個弧線
// addArcWithCenter是添加一個弧線,共同點就是參數(shù)都一樣
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;

// 使用這個方法起始點與終點將相連
- (void)closePath;

// 移除所有坐標點
- (void)removeAllPoints;

// 添加一個paths UIBezierPath
- (void)appendPath:(UIBezierPath *)bezierPath;

// 創(chuàng)建 并返回一個與當前路徑相反的新的貝塞爾路徑對象
- (UIBezierPath *)bezierPathByReversingPath;

// 用指定的仿射變換矩陣變換路徑的所有點
- (void)applyTransform:(CGAffineTransform)transform;

// 該值指示路徑是否有任何有效的元素。
@property(readonly,getter=isEmpty) BOOL empty;

// 路徑包括的矩形
@property(nonatomic,readonly) CGRect bounds;

// 圖形路徑中的當前點
@property(nonatomic,readonly) CGPoint currentPoint;

// 接收器是否包含指定的點
- (BOOL)containsPoint:(CGPoint)point;

// 線寬
@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;

// 最大斜接長度   斜接長度指的是在兩條線交匯處內(nèi)角和外角之間的距離
@property(nonatomic) CGFloat miterLimit; 
// 最大斜接長度   斜接長度指的是在兩條線交匯處內(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 當前圖形上下文中的路徑操作:
// 填充顏色
- (void)fill;

// 利用當前繪圖屬性沿著接收器的路徑繪制
- (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ū)域 該路徑是帶有剪切路徑的當前繪圖上下文。使得其成為我們當前的剪切路徑
- (void)addClip;

實踐


- (void)drawRect:(CGRect)rect {

    UIColor *brushColor = [UIColor whiteColor];
    
    // 根據(jù)一個Rect 畫一個矩形曲線
    UIBezierPath *rectangular = [UIBezierPath bezierPathWithRect:CGRectMake(5, 5, 30, 30)];
    [PNRed set];
    [rectangular fill];
    [brushColor set];
    [rectangular stroke];
    
    // 根據(jù)一個Rect 畫一個橢圓曲線  Rect為正方形時 畫的是一個圓
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(40, 5, 50, 30)];
    [PNBlue set];
    [oval fill];
    [brushColor set];
    [oval stroke];
    
    // 根據(jù)一個Rect 畫一個圓角矩形曲線 (Radius:圓角半徑)    當Rect為正方形時且Radius等于邊長一半時 畫的是一個圓
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(95, 5, 40, 30)cornerRadius:5];
    [PNStarYellow set];
    [roundedRect fill];
    [brushColor set];
    [roundedRect stroke];
    
    // 根據(jù)一個Rect 針對四角中的某個或多個角設(shè)置圓角
    UIBezierPath *roundedRect2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(140, 5, 40,30) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10,50)];
    [PNFreshGreen set];
    [roundedRect2 fill];
    [brushColor set];
    [roundedRect2 stroke];

    // 以某個中心點畫弧線
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 15) radius:20startAngle:0 endAngle:degreesToRadian(90) clockwise:YES];
    [brushColor set];
    [arcPath stroke];
    
    // 添加一個弧線
    UIBezierPath *arcPath2 = [UIBezierPath bezierPath];
    [arcPath2 moveToPoint:CGPointMake(230, 30)];
    [arcPath2 addArcWithCenter:CGPointMake(265, 30) radius:25 startAngle:degreesToRadian(180)endAngle:degreesToRadian(360) clockwise:YES];
    // 添加一個UIBezierPath
    [arcPath2 appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(265, 30) radius:20startAngle:0 endAngle:M_PI*2 clockwise:YES]];
    [PNStarYellow set];
    [arcPath2 stroke];
    
    // 根據(jù)CGPath創(chuàng)建并返回一個新的UIBezierPath對象
    UIBezierPath *be = [self bezierPathWithCGPath];
    [PNRed set];
    [be stroke];
    
    // 三角形
    UIBezierPath *triangle = [UIBezierPath bezierPath];
    [triangle moveToPoint:CGPointMake(145, 165)];
    [triangle addLineToPoint:CGPointMake(155, 185)];
    [triangle addLineToPoint:CGPointMake(135, 185)];
    [PNStarYellow set];
    [triangle fill];
//    [triangle stroke];
    [triangle closePath];
    
    // 二次貝塞爾曲線
    UIBezierPath *quadBe = [UIBezierPath bezierPath];
    [quadBe moveToPoint:CGPointMake(30, 150)];
    [quadBe addQuadCurveToPoint:CGPointMake(130, 150) controlPoint:CGPointMake(30, 70)];
    
    UIBezierPath *quadBe2 = [UIBezierPath bezierPath];
    [quadBe2 moveToPoint:CGPointMake(160, 150)];
    [quadBe2 addQuadCurveToPoint:CGPointMake(260, 150) controlPoint:CGPointMake(210, 50)];
    [quadBe2 appendPath:quadBe];
    quadBe2.lineWidth = 1.5f;
    quadBe2.lineCapStyle = kCGLineCapSquare;
    quadBe2.lineJoinStyle = kCGLineJoinRound;
    [brushColor set];
    [quadBe2 stroke];
    
    // 三次貝塞爾曲線
    UIBezierPath *threePath = [UIBezierPath bezierPath];
    [threePath moveToPoint:CGPointMake(30, 250)];
    [threePath addCurveToPoint:CGPointMake(260, 230) controlPoint1:CGPointMake(120, 180)controlPoint2:CGPointMake(150, 260)];
    threePath.lineWidth = 1.5f;
    threePath.lineCapStyle = kCGLineCapSquare;
    threePath.lineJoinStyle = kCGLineJoinRound;
    [brushColor set];
    [threePath stroke];
}

- (UIBezierPath *)bezierPathWithCGPath {
    UIBezierPath *framePath;
    CGFloat arrowWidth = 14;
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGRect rectangle = CGRectInset(CGRectMake(0, 0, CGRectGetWidth(self.bounds),CGRectGetWidth(self.bounds)), 3,3);
    
    CGPoint p[3] = {
        
    {CGRectGetMidX(self.bounds)-arrowWidth/2, CGRectGetWidth(self.bounds)-6},
        
    {CGRectGetMidX(self.bounds)+arrowWidth/2, CGRectGetWidth(self.bounds)-6},
        
    {CGRectGetMidX(self.bounds), CGRectGetHeight(self.bounds)-4}
        
    };
    
    CGPathAddRoundedRect(path, NULL, rectangle, 5, 5);
    
    CGPathAddLines(path, NULL, p, 3);
    
    CGPathCloseSubpath(path);
    // 根據(jù)CGPath創(chuàng)建并返回一個新的UIBezierPath對象
    framePath = [UIBezierPath bezierPathWithCGPath:path];
    
    CGPathRelease(path);
    
    return framePath;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容