UIBezierPath(一)

直线

1.基本方法

-(void)addline
{
    // 1.获取图形上下文
    CGContextRef ref = UIGraphicsGetCurrentContext();
    // 2.描述路径
    // 创建路径
    CGMutablePathRef path = CGPathCreateMutable();
    //在路径上添加起点
    CGPathMoveToPoint(path, NULL, 50, 50);
    // 添加一条线(另一点坐标)
    CGPathAddLineToPoint(path, NULL, 200, 200);
    
    // 3.把路径添加到图形上下文
    CGContextAddPath(ref, path);
    // 4.渲染上下文
    CGContextStrokePath(ref);
    
}

2底层默认创建了路径

#pragma mark - 默认底层封装了路径
-(void)addline1
{
    
    CGContextRef ref = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(ref, 50, 50);
    CGContextAddLineToPoint(ref, 200, 200);
    CGContextStrokePath(ref);
    
}

3贝塞尔画直线

#pragma mark - UIBezierPath曲线画直线
-(void)addline2{
    
    UIBezierPath * path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(200, 200)];
    [path stroke];
    
}

效果图


直线

折线

1.一样的颜色

#pragma mark - 折线
-(void)addline3
{
    CGContextRef ref = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(ref, 50, 50);
    CGContextAddLineToPoint(ref, 100, 50);
    CGContextAddLineToPoint(ref, 100, 200);
    CGContextAddLineToPoint(ref, 200, 100);
    //设置颜色
    [[UIColor redColor] setStroke];
    // 设置线宽
    CGContextSetLineWidth(ref, 6);
    // 设置角的连接样式
    CGContextSetLineJoin(ref, kCGLineJoinBevel);
    // 设置顶角样式
    CGContextSetLineCap(ref, kCGLineCapRound);
    // 渲染
    CGContextStrokePath(ref);
}
折线

2.两条线设置各自的样式

#pragma mark - 两条线
-(void)addtwoLine
{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 100)];
    path.lineWidth = 10;
    [[UIColor redColor] setStroke];
    [path stroke];
    
    //画i第二条线
    UIBezierPath * path1 = [UIBezierPath bezierPath];
    [path1 moveToPoint:CGPointMake(50, 80)];
    [path1 addLineToPoint:CGPointMake(100, 130)];
    path1.lineWidth = 8;
    [[UIColor purpleColor] setStroke];
    [path1 stroke];    
}
两条线

曲线

#pragma mark - 画曲线
-(void)addCurve
{
    
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ref, 0, 50);
    /**
     CGFloat cpx // 控制点的X
     CGFloat cpy // 控制点的Y
     */
    CGContextAddQuadCurveToPoint(ref, 50, 5 , 100, 50);
    
    CGContextStrokePath(ref);

}
曲线

圆角长方形,椭圆

#pragma mark - 圆角长方形,椭圆
-(void)circleLine
{
    //圆角长方形
    //    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 150, 150) cornerRadius:75];
    //    [path stroke];
    
    // 椭圆
    UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 180, 100)];
    [path fill];
    
    
}
椭圆

扇形

#pragma mark - 画扇形
-(void)addLine4
{
    /**
     画圆弧
     CGPoint 中心点
     radius 半径
     startAngle 开始弧度
     endAngle 结束弧度
     clockwise YES 顺时针 NO逆时针
     
     
     */
    
    CGPoint center = CGPointMake(100, 100);
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:80 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    //画扇形
    [path addLineToPoint:center];
    
    // 封闭图形
    //    [path closePath];
    
    
    // 渲染
    //    [path stroke];
    
    // 填充的话默认就封闭图形
    [path fill];
    

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

推荐阅读更多精彩内容

  • UIBezierPath介绍 基本介绍: UIBezierPath可以用来创建矢量的路径和图形,使用此类可以定义各...
    BigDaddy_阅读 2,578评论 0 7
  • UIBezierPath详解 我在写本篇文章之前,也没有系统学习过贝塞尔曲线,只是曾经某一次的需求需要使用到,才临...
    白水灬煮一切阅读 1,197评论 0 4
  • 基础知识 使用UIBezierPath可以创建基于矢量的路径,此类是Core Graphics框架关于路径的封装。...
    十里桃花不及你阅读 879评论 0 5
  • 公司2017年中总结之后,进行了为期一天的户外拓展活动,活动共计31人,分为三组游戏对抗,在游戏过程中看出了公司的...
    匠心writting阅读 740评论 0 0
  • 昨晚看到自己的好友在朋友圈发了条状态-人生已经这么艰难了我为什么还要委屈自己。当我看到这条状态的时候,我大概想到,...
    伊予yisheng阅读 273评论 0 0