IOS開(kāi)發(fā)之——繪制基本形狀

文章搬運(yùn)來(lái)源:https://blog.csdn.net/Calvin_zhou/article/details/111941368
作者:PGzxc

對(duì)iOS開(kāi)發(fā)感興趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相關(guān)方面的知識(shí),群里還有我整理的有關(guān)于面試的一些資料,歡迎大家加群,大家一起開(kāi)車

一 概述

本文介紹基本圖形的繪制:

  • 三角形
  • 矩形
  • 圓形
  • 橢圓
  • 圓弧
  • 封閉圓弧

二 繪制三角形

2.1 繪制代碼

- (void)drawRect:(CGRect)rect {

    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    UIBezierPath *path=[UIBezierPath bezierPath];
    CGPoint startP=CGPointMake(10, 10);
    [path moveToPoint:startP];
    [path addLineToPoint:CGPointMake(125, 125)];
    [path addLineToPoint:CGPointMake(240, 10)];
    [path closePath];
    //[path addLineToPoint:startP];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    [[UIColor blueColor]setFill];
    [[UIColor redColor]setStroke];
    CGContextSetLineWidth(ctx, 15);
    //4.渲染上下文
    //CGContextStrokePath(ctx);
    //CGContextFillPath(ctx);
    CGContextDrawPath(ctx,kCGPathFillStroke); 
}

2.2 給三角形添加文字

- (UILabel *)label{
    if (_label==nil) {
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 250, 100)];
        label.text=@"s";
        label.textColor=[UIColor yellowColor];
        label.font=[UIFont systemFontOfSize:60];
        label.textAlignment=NSTextAlignmentCenter;
        [self addSubview:label];
    }
    return _label;
}
- (void)awakeFromNib
{
    self.label;
}

2.3 效果圖

三 繪制矩形

3.1 代碼

- (void)drawRect:(CGRect)rect {
    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    UIBezierPath *paht=[UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, paht.CGPath);
    //4.渲染上下文
    CGContextStrokePath(ctx);
}

3.2 效果圖

四 繪制圓

4.1 代碼1

- (void)drawRect:(CGRect)rect {

    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 200)];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.渲染上下文
    CGContextStrokePath(ctx);

}

4.2 代碼2

- (void)drawRect:(CGRect)rect {

    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    UIBezierPath *paht=[UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)];
    paht=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:100];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, paht.CGPath);
    //4.渲染上下文
    CGContextStrokePath(ctx);

}

4.3 效果圖

五 繪制橢圓

5.1 代碼

- (void)drawRect:(CGRect)rect {

    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 100)];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.渲染上下文
    CGContextStrokePath(ctx);
}

5.2 效果圖

六 圓弧

6.1 代碼

- (void)drawRect:(CGRect)rect {
    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    CGPoint center=CGPointMake(125, 125);
    CGFloat radius=100;
    CGFloat startA=0;
    CGFloat endA=M_PI;
    UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.渲染上下文
    CGContextStrokePath(ctx);

}

6.2 效果圖

七 封閉圓弧

7.1 代碼

- (void)drawRect:(CGRect)rect {
    //1.獲取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.拼接路徑
    CGPoint center=CGPointMake(125, 125);
    CGFloat radius=100;
    CGFloat startA=0;
    CGFloat endA=M_PI_2;
    UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.渲染上下文
    //CGContextStrokePath(ctx);
    CGContextFillPath(ctx);

}

7.2 效果圖

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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