查看: 40|回复: 0

IOS开发之——绘制基本形状

[复制链接]

1

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2010-10-14
发表于 2021-2-19 16:21:00 | 显示全部楼层 |阅读模式

文章搬运来源:https://blog.csdn.net/Calvin_zhou/article/details/111941368
作者:PGzxc

对iOS开发感兴趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎大家加群,大家一起开车

一 概述

本文介绍基本图形的绘制:

  • 三角形
  • 矩形
  • 圆形
  • 椭圆
  • 圆弧
  • 封闭圆弧

二 绘制三角形

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 效果图



来源:https://www.cnblogs.com/fadaijun/p/14416685.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部