使用UIBezierPath進行簡單的圖形繪制

項目告一段落。閑下來自己看看了貝塞爾進行圖形繪制,項目中沒有過太多,但是看一個技術(shù)群討論過繪圖,自己在網(wǎng)上看了許多,學(xué)習(xí)了一下。

寫個類繼承UIView 我的JYJ_BezierPathView : UIView
然后寫個初始化方法

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

繪圖需要寫在這個方法里- (void)drawRect:(CGRect)rect

- (void)drawRect:(CGRect)rect {
    [self demo1];
    //[self demo2];
    //[self demo3];
    //[self demo4];
    //[self demo5];
    //[self demo6];

   
}

demo1

demo1.png

代碼

- (void)demo1 {
    // 初始化一個UIBezierPath對象.
    UIBezierPath *bPath = [UIBezierPath bezierPath];
    // 線寬.
    bPath.lineWidth = 5;
    // 拐點處理.
    bPath.lineCapStyle = kCGLineCapRound;
    // 終點處理.
    bPath.lineJoinStyle = kCGLineCapRound;
    // 添加線上的點.
    [bPath moveToPoint:CGPointMake(w / 2, 0.0)];
    [bPath addLineToPoint:CGPointMake(w, h / 2)];
    [bPath addLineToPoint:CGPointMake(w / 2, h)];
    [bPath addLineToPoint:CGPointMake(0.0, h / 2)];
    UIColor *fillColor = [UIColor redColor];
    //繪制線線的里面的顏色
    [fillColor setFill];
    UIColor *strokeColor = [UIColor cyanColor];
    //繪制線的顏色
    [strokeColor setStroke];
    [bPath closePath];
    // 填充內(nèi)部顏色.
    [bPath fill];
    
    // 繪制線.
    [bPath stroke];
 
}

demo2

demo2.png

代碼

- (void)demo2 {
    //創(chuàng)建矩形
    UIBezierPath *bPath = [UIBezierPath bezierPathWithRect:CGRectMake(30, 30, w - 60, h - 60)];
    bPath.lineWidth = 10;
    UIColor *stokeColor = [UIColor redColor];
    [stokeColor setStroke];
    //繪制線
    [bPath stroke];
}

demo3

demo3.png

代碼

- (void)demo3 {
    ///這個芳法,是做一個內(nèi)切的曲線
    // 圓形就是寬高相等
    UIBezierPath *bPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, w - 10, w - 10)];
    UIColor *stokeColor = [UIColor redColor];
    [stokeColor setStroke];
    bPath.lineWidth = 10;
    [bPath stroke];
}

demo4

demo4.png

代碼

- (void)demo4 {
    //繪制一條弧線 用 bezierPathWithArcCenter  這個方法
//    看下各個參數(shù)的意義:
//    center:圓心的坐標(biāo)
//    radius:半徑
//    startAngle:起始的弧度 從開始的角度
//    endAngle:圓弧結(jié)束的弧度  從結(jié)束的角度算
//    clockwise:YES為順時針,No為逆時針
    UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(w / 2,  h / 2) radius:130 startAngle: M_PI / 6  endAngle: M_PI / 6 * 5 clockwise:YES];
    bPath.lineWidth = 5;
    [bPath stroke];
}

demo5

demo5.png

代碼

- (void)demo5 {
    //繪制二次貝塞爾曲線,moveToPoint,addQuadCurveToPoint這兩個搭配
    
    // 二次貝塞爾的曲線支持
    UIBezierPath *bPath = [UIBezierPath bezierPath];
    // 開始的點
    [bPath moveToPoint:CGPointMake(0, h)];
    //終止點CurveToPoint,控制點controlPoint
    [bPath addQuadCurveToPoint:CGPointMake(w, h) controlPoint:CGPointMake(w / 2, 0)];
    [bPath fill];

}

demo6

demo6.png

代碼

- (void)demo6 {
    //三次貝塞爾曲線
    UIBezierPath *bPath = [UIBezierPath bezierPath];
    [bPath moveToPoint:CGPointMake(0, h / 2)];
    [bPath addCurveToPoint:CGPointMake(w, h/2) controlPoint1:CGPointMake(w/2, 0) controlPoint2:CGPointMake(w/2, h)];
    bPath.lineWidth = 10;
    UIColor *strokeColor = [UIColor cyanColor];
    [strokeColor setStroke];
        [bPath stroke];
}

簡單系統(tǒng)的學(xué)習(xí)了一下,這也是從網(wǎng)上學(xué)到的。項目中總歸是要碰到的,不如早點學(xué)學(xué)。

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

推薦閱讀更多精彩內(nèi)容

  • 這篇文章介紹UIBezierPath的詳細(xì)的使用, 以及一些細(xì)節(jié)! 創(chuàng)建一個XTBezierPath繼承于UIVi...
    夏天然后閱讀 1,604評論 2 17
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,141評論 5 13
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,566評論 6 30
  • 前言 本文只要描述了iOS中的Core Animation(核心動畫:隱式動畫、顯示動畫)、貝塞爾曲線、UIVie...
    GitHubPorter閱讀 3,659評論 7 11
  • 題目背景 Farmer John每年有很多柵欄要修理。他總是騎著馬穿過每一個柵欄并修復(fù)它破損的地方。 題目描述 J...
    寒冰的薩蘭閱讀 399評論 0 0