UIBezierPath

UIBezierPath是CoreGraphics的封裝,使用它可以完成大部分的繪圖操作。

使用可以簡單的分為三步:

  • 創(chuàng)建path
  • 添加路徑到path
  • 將path繪制出來。

類要繼承自UIView

- (void)drawRect:(CGRect)rect
{
    // 設(shè)置顏色
    UIColor *color = [UIColor redColor];
    [color set];
    // 初始化
    UIBezierPath* aPath = [UIBezierPath bezierPath];
    // 設(shè)置線寬
    aPath.lineWidth = 2.0;
    // 線條處理
    aPath.lineCapStyle = kCGLineCapRound; //線條拐角
    aPath.lineJoinStyle = kCGLineCapRound; //終點處理
   // 起始點
    [aPath moveToPoint:CGPointMake(100.0, 100.0)];
    // 畫線
    [aPath addLineToPoint:CGPointMake(300.0, 100.0)]; // 第一點
    [aPath addLineToPoint:CGPointMake(300.0, 300.0)]; // 第二點
    [aPath addLineToPoint:CGPointMake(100.0, 300.0)]; // 第三點
    //通過調(diào)用closePath方法得到的
    [aPath closePath];
    // 根據(jù)坐標(biāo)點連線
    [aPath stroke];


}
四邊形

在上邊中將[aPath stroke]換成一個

 // 填充閉合圈子里面的顏色
 [aPath fill];
增加fill之后的情景啦

當(dāng)然也有直接的畫四邊行的初始化化方法

- (void)drawRect:(CGRect)rect
{
    // 設(shè)置顏色
    UIColor *color = [UIColor redColor];
    [color set];
    // 初始化
    UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 200)];
    // 設(shè)置線寬
    aPath.lineWidth = 2.0;
    // 線條處理
    aPath.lineCapStyle = kCGLineCapRound; //線條拐角
    aPath.lineJoinStyle = kCGLineCapRound; //終點處理
    // 填充
    [aPath fill];

}

上面的效果同上。

 // 初始化
  UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 200)];

將上邊的bezierPathWithRect:換成bezierPathWithOvalInRect:,就變成了一個以右上角為(100,100)直接100的圓啦。
當(dāng)然畫橢圓,直接改成CGRectMake(60, 100, 300, 200)

 UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(60, 100, 300, 200)];
橢圓

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center // 圓弧的中心
                                  radius:(CGFloat)radius // 半徑
                              startAngle:(CGFloat)startAngle // 開始的角度
                                endAngle:(CGFloat)endAngle  // 結(jié)束的角度
                               clockwise:(BOOL)clockwise  // 是否順時針方向

直接實現(xiàn)

- (void)drawRect:(CGRect)rect
{
    UIColor *color = [UIColor redColor];
    [color set]; //設(shè)置線條顏色
    
    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200)
                                                         radius:100
                                                     startAngle:0
                                                       endAngle:(M_PI * 270)/180
                                                      clockwise:YES];

    
    [aPath fill];
}
270度的弧

貝塞爾曲線

曲線段在當(dāng)前點開始,在指定的點結(jié)束。曲線的形狀有開始點,結(jié)束點,一個或者多個控制點的切線定義。

貝塞爾曲線
- (void)drawRect:(CGRect)rect
{
    UIColor *color = [UIColor greenColor];
    [color set];
    UIBezierPath* aPath = [UIBezierPath bezierPath];
    aPath.lineWidth = 2.0;
    aPath.lineCapStyle = kCGLineCapRound; //線條拐角
    aPath.lineJoinStyle = kCGLineCapRound; //終點處理
    [aPath moveToPoint:CGPointMake(50, 100)];
//    [aPath addQuadCurveToPoint:CGPointMake(300, 120) controlPoint:CGPointMake(100, 20)];
    [aPath addCurveToPoint:CGPointMake(300, 50) controlPoint1:CGPointMake(120, 30) controlPoint2:CGPointMake(230, 200)];
    [aPath stroke];
}
三次貝塞爾曲線

備注

看了martin的直播后,覺的下列這兩個網(wǎng)址還是推薦的,特別是第一個,讓我們更好理解貝塞爾曲線。

參考

http://blog.csdn.net/crayondeng/article/details/11093689
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIBezierPath_class/

最后編輯于
?著作權(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)容