贝塞尔曲线

贝塞尔曲线学习

上篇简单的学习了一下画线的知识,其实有心的人可能已经发现了,关于绘制这块,系统已经提供了几个内置的方法:

  • (instancetype)bezierPathWithRect:(CGRect)rect;
  • (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
  • (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
  • (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
  • (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
  • (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
    今天就简单的介绍一下这几个方法

1:画矩形
• (instancetype)bezierPathWithRect:(CGRect)rect;
• 系统方法,画矩形
• rect: 矩形的Frame

  • (void)drawRect:(CGRect)rect
    {
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制矩形,需要传入绘制的矩形的Frame
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 280, 280)];
    // 设置线宽度
    bezierPath.lineWidth = 10;
    // 设置线两头样式
    bezierPath.lineCapStyle = kCGLineCapRound;

    // 开始绘制
    [bezierPath stroke];
    }

2:画矩形,圆角矩形
• (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
• 系统方法,绘制一个圆角的矩形
• rect: 矩形的Frame
• cornerRadius: 圆角的半径

  • (void)drawRect:(CGRect)rect
    {
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制一个圆角矩形
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280)
    cornerRadius:30];
    // 设置线宽度
    bezierPath.lineWidth = 10;

    // 开始绘制
    [bezierPath stroke];
    }

3:画矩形,部分圆角的矩形
• (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
• 画一个部分圆角的矩形
• rect: 需要画的矩形的Frame
• corners: 哪些部位需要画成圆角
• cornerRadii: 圆角的Size

  • (void)drawRect:(CGRect)rect
    {
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制一个部分圆角的矩形,左上、右下
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280)
    byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight
    cornerRadii:CGSizeMake(10, 10)];
    // 设置线宽度
    bezierPath.lineWidth = 10;

    // 开始绘制
    [bezierPath stroke];
    }

4:画圆,内切圆
• (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
• 画圆,这个方法绘制的是一个矩形的内切圆
• rect: 矩形的Frame

  • (void)drawRect:(CGRect)rect
    {
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制内切圆,需要传入绘制内切圆的矩形的Frame
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 280, 280)];
    // 设置线宽度
    bezierPath.lineWidth = 10;
    // 设置线两头样式
    bezierPath.lineCapStyle = kCGLineCapRound;

    // 开始绘制
    [bezierPath stroke];
    }

5:画圆弧
• (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
• center: 圆心坐标
• radius: 圆的半径
• startAngle: 绘制起始点角度
• endAngle: 绘制终点角度
• clockwise: 是否顺时针

  • (void)drawRect:(CGRect)rect
    {
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制一个圆弧
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
    radius:110
    startAngle:0
    endAngle:M_PI_2
    clockwise:NO];
    // 设置线宽度
    bezierPath.lineWidth = 10;

    // 开始绘制

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIBezierPath主要用来绘制矢量图形,它是基于Core Graphics对CGPathRef数据类型和pa...
    胡子仙森阅读 5,052评论 0 50
  • UIBezierPath主要用来绘制矢量图形,它是基于Core Graphics对CGPathRef数据类型和pa...
    jgj1218阅读 2,361评论 0 0
  • 谈谈贝塞尔曲线 最近在做项目的时候,需要用到一个动画,非常简单的动画,简单到就是直接对一个View做平移… 然而虽...
    雨润听潮阅读 11,308评论 1 16
  • 应景秋日自古多愁。 抱怨天太高了,地太厚了, 你却不动了。 都说宠物随人, 大概这辈子不能再有。 失去的理由,归根...
    陈紫杉阅读 1,469评论 0 0
  • 7/1-9/30达成目标:智慧增长(遇到任何事都能第一时间运用笔的道理去应对)+财富增长(投资项目第一轮达到预期收...
    快乐小屋刘丽华阅读 1,404评论 0 2