ios Quartz2D使用(畫一些簡單的圖形)(2)

一、畫圖形(這些都是要繼承uiview 重寫(void)drawRect:(CGRect)rect 方法,后面會說在viewcontroller中如何畫)

1.圓形

  // 圓角矩形
  //   UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];


  // 圓弧
    // Center:圓心
    // startAngle:弧度
    // clockwise:YES:順時針 NO:逆時針
    // 扇形
    CGPoint center = CGPointMake(125, 125);
     UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    
    // 添加一根線到圓心
    [path addLineToPoint:center];
    
    // 封閉路徑,關(guān)閉路徑:從路徑的終點(diǎn)到起點(diǎn)
//    [path closePath];
    
    // 就是畫一個圓弧
    //    [path stroke];
    
    // 填充:必須是一個完整的封閉路徑,默認(rèn)就會自動關(guān)閉路徑
    //  畫一個圓餅
    [path fill];

2.柱狀圖

        w = rect.size.width / (2 * arr.count - 1);
        x = 2 * w * i;
        h = [arr[i] floatValue] / 100.0 * rect.size.height;
        y = rect.size.height - h;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];
        
        [[self colorRandom] set];
        [path fill];

3.圖片和文字繪制到特定圖形上面

// 超出裁剪區(qū)域的內(nèi)容全部裁剪掉
    // 注意:裁剪必須放在繪制之前
//    UIRectClip(CGRectMake(0, 0, 50, 50));
    
    UIImage *image = [UIImage imageNamed:@"001"];
    
    // 默認(rèn)繪制的內(nèi)容尺寸跟圖片尺寸一樣大
//    [image drawAtPoint:CGPointZero];
    // 在哪個范圍中繪制圖片,就是填充 會拉伸
//    [image drawInRect:rect];
    // 平鋪整個畫布
    [image drawAsPatternInRect:rect];
文字
   // 繪制文字
    
    NSString *str = @"asfdsfsdf";
    // 文字的起點(diǎn)
    // Attributes:文本屬性

    NSMutableDictionary *textDict = [NSMutableDictionary dictionary];

    // 設(shè)置文字顏色
    textDict[NSForegroundColorAttributeName] = [UIColor redColor];

    // 設(shè)置文字字體
    textDict[NSFontAttributeName] = [UIFont systemFontOfSize:30];

    // 設(shè)置文字的空心顏色和寬度

    textDict[NSStrokeWidthAttributeName] = @3;

    textDict[NSStrokeColorAttributeName] = [UIColor yellowColor];

    // 創(chuàng)建陰影對象
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor greenColor];
    shadow.shadowOffset = CGSizeMake(4, 4);
    shadow.shadowBlurRadius = 3;
    textDict[NSShadowAttributeName] = shadow;

    // 富文本:給普通的文字添加顏色,字體大小
    [str drawAtPoint:CGPointZero withAttributes:textDict];
    // 繪制在某個區(qū)域中,文字長會換行
//       [str drawInRect:rect withAttributes:textDict];

4.圖形的一些操作

 // 1.獲取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.描述路徑
   UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
    [[UIColor redColor] set];
    // 上下文矩陣操作
    // 注意:矩陣操作必須要在添加路徑之前
    //  平移
    CGContextTranslateCTM(ctx, 100, 50);
    // 縮放
    CGContextScaleCTM(ctx, 0.5, 0.5);
    // 旋轉(zhuǎn)
    CGContextRotateCTM(ctx, M_PI_4);
    // 3.把路徑添加上下文
    CGContextAddPath(ctx, path.CGPath);
    [[UIColor redColor] set];
    
    
    // 4.渲染上下文
    CGContextFillPath(ctx);

demo地址

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

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

  • Quartz2D以及drawRect的重繪機(jī)制字?jǐn)?shù)1487 閱讀21 評論1 喜歡1一、什么是Quartz2D Q...
    PurpleWind閱讀 909評論 0 3
  • 本人寫這個純粹是為了記住ios中繁瑣的動畫,一個各種各樣的路徑,上年紀(jì)了,記不住了。 繪圖的步驟: 1.獲取上下文...
    攝影師諾風(fēng)閱讀 527評論 0 0
  • 什么是Quartz2D 是一個二維的繪圖引擎,同時支持iOS和Mac系統(tǒng) Quartz2D的API是純C語言的,它...
    Mario_ZJ閱讀 659評論 0 1
  • 在布達(dá)拉宮山下,時不時能見到朝圣的藏人。他們每走兩步都要雙手合一,舉過頭頂,然后跪下,雙手落地,再向前滑動,...
    我是大王_48f5閱讀 414評論 0 1
  • 作為iOS開發(fā),XCode一天編譯幾十上百遍是常事。cmd+B,兩指一按,嘩啦啦的就開始build了。但是,我們是...
    微微笑的蝸牛閱讀 6,643評論 1 51

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