UIBezierPath - 貝塞爾曲線

一般只能在drawRet中繪制。但是結(jié)合 CAShapeLayer,繪制到layer上在add到View上就能隨機(jī)繪制了。這里只講UIBezierPath。CAShapeLayer 很簡(jiǎn)單,看另外一篇好了。

指定圖形

    UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 400, 100)];// 矩形
    UIBezierPath *path2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(90, 90, 120, 120)];// 內(nèi)接圓
    UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(90, 90, 120, 120) cornerRadius:12];// 圓角矩形
    UIBezierPath *path4 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(90, 90, 120, 120) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 2)];// 選擇性圓角矩形
    /*
     typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
     UIRectCornerTopLeft     = 1 << 0,
     UIRectCornerTopRight    = 1 << 1,
     UIRectCornerBottomLeft  = 1 << 2,
     UIRectCornerBottomRight = 1 << 3,
     UIRectCornerAllCorners  = ~0UL
     };// 哪個(gè)角
     */
    
    UIBezierPath *path5 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(120, 120) radius:120 startAngle:0 endAngle:M_PI/2 clockwise:NO];// 弧線。(0 - 正x軸,2M_PI 是一圈。clockwise :是順逆時(shí)針)
    

// 注意 ,只有圖形是不夠滴,還需要對(duì)圖形添加屬性,以及繪制,下面講。

自定義 圖形

    // 初始化
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(100, 100)];// 起點(diǎn)
    
    [path addLineToPoint:CGPointMake(120,120)];// 直線終點(diǎn)
    [path addCurveToPoint:CGPointMake(100, 200) controlPoint1:CGPointMake(120, 220) controlPoint2:CGPointMake(100, 300)];// 2次曲線
    [path addQuadCurveToPoint:CGPointMake(100, 200) controlPoint:CGPointMake(140, 230)];// 1次曲線
    [path addArcWithCenter:CGPointMake(200, 200) radius:80 startAngle:0 endAngle:M_PI clockwise:YES];// 圓弧
    
    [path closePath];// 首位相連

    [path appendPath:path2];// 拼接路徑
    path = [path bezierPathByReversingPath];// 路徑方向相反,圖形是一樣的
    
    [path removeAllPoints];// 刪除所有路徑

    // 3D變化,需要好好研究0.0
    [path applyTransform:CGAffineTransform];
   

路徑屬性 與 繪制

  • 基本繪制
     path.lineWidth = 1.;// 線寬
    
    // 顏色 填充色,描邊色
    [[UIColor redColor] setFill];
    [[UIColor greenColor] setStroke];
    
    // 繪制
    [path fill];
    [path stroke];
    
    // 裁剪,作用不明
    [path addClip];
    
  • 更多好玩的屬性
    path.miterLimit = 2.;// 防止轉(zhuǎn)折處尖叫過長
    path.flatness = 2.;// 根據(jù)彎曲程度,決定渲染精度?
    path.usesEvenOddFillRule = NO;// 這個(gè)牛逼了,圖形復(fù)雜是填充顏色的一種規(guī)則。類似棋盤。
    path.lineCapStyle = kCGLineCapRound;// 兩端
    /*
     typedef CF_ENUM(int32_t, CGLineCap) {
     kCGLineCapButt,
     kCGLineCapRound,
     kCGLineCapSquare
     };
     */
    
    path.lineJoinStyle = kCGLineJoinRound;// 轉(zhuǎn)折
    /*
     typedef CF_ENUM(int32_t, CGLineJoin) {
     kCGLineJoinMiter,
     kCGLineJoinRound,
     kCGLineJoinBevel
     };
     */

獲取 屬性

@property(nonatomic) CGPathRef CGPath;
- (CGPathRef)CGPath NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;

@property(readonly,getter=isEmpty) BOOL empty;
@property(nonatomic,readonly) CGRect bounds;
@property(nonatomic,readonly) CGPoint currentPoint;
- (BOOL)containsPoint:(CGPoint)point;

其他

  • addClip:作用不明
  • (void)applyTransform:(CGAffineTransform)transform:水太深。
    1
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 關(guān)于 UIBezierPath UIBezierPath這個(gè)類在UIKit中, 是Core Graphics框架關(guān)...
    劉光軍_MVP閱讀 59,219評(píng)論 18 202
  • TopicList 一, UIBezierPath 簡(jiǎn)介 二, UIBezierPath 初始化方法 三, UIB...
    爨鄉(xiāng)的云閱讀 24,825評(píng)論 4 56
  • 引言: UIBezierPath類只是CGPathRef數(shù)據(jù)類型和path繪圖屬性的一個(gè)封裝。 參考: 貝塞爾曲線...
    楊大蝦閱讀 1,076評(píng)論 0 0
  • 說起這個(gè)貝塞爾曲線,其實(shí)可以實(shí)現(xiàn)很多功能的:曲線圖、折線圖、進(jìn)度條、畫板、不規(guī)則圖形等等。下邊我們就來看看這條線。...
    和玨貓閱讀 2,962評(píng)論 3 19
  • “眾包”這一概念實(shí)際上是源于對(duì)企業(yè)創(chuàng)新模式的反思。傳統(tǒng)的產(chǎn)品創(chuàng)新方法是:首先由生產(chǎn)商對(duì)市場(chǎng)進(jìn)行調(diào)查,然后根據(jù)調(diào)查結(jié)...
    猿團(tuán)閱讀 148評(píng)論 0 0

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