ios基本线条绘制

/*

 作用:专门用来绘图

 什么时候调用:系统自动调用,当View显示的时候调用

 param rect:当前view的bounds

 */

-(void)drawRect:(CGRect)rect{

    //1、在drawRect方法当中系统已经帮你创建一个跟view相关联的上下文(Layer)


    //[self drawLine];


    //画曲线


    //1、获取上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //2、绘制路径

    UIBezierPath *path = [UIBezierPath bezierPath];

    //画曲线

    [pathmoveToPoint:CGPointMake(50, 200)];

    //添加一根曲线到某一点

    [pathaddQuadCurveToPoint:CGPointMake(250, 200) controlPoint:CGPointMake(50, 50)];

    //3、把绘制的内容保存到上下文当中

    CGContextAddPath(ctx, path.CGPath);

    //4、把上下文内容显示到view上

    CGContextStrokePath(ctx);

}

//画直线

-(void)drawLine{

    //1、获取上下文(获取、创建上下文都以UIGraphic开头)

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //2、绘制路径

    UIBezierPath *path = [UIBezierPath bezierPath];

    //2.1:设置起点

    CGPointbeginPoint =CGPointMake(50,280);

    CGPointendPoint =CGPointMake(250,50);

    [pathmoveToPoint:beginPoint];

    //2.2:添加一根线到终点

    [pathaddLineToPoint:endPoint];


    //画第二条线

    //[path moveToPoint:CGPointMake(100, 200)];

    [pathaddLineToPoint:CGPointMake(250, 150)];


    //上下文状态

    //设置线宽

    CGContextSetLineWidth(ctx, 10);

    //设置线的连接样式

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    //设置线的顶角样式

    CGContextSetLineCap(ctx, kCGLineCapRound);

    //设置颜色

    [[UIColor redColor] set];



    //3、把绘制的内容保存到上下文当中

    //CGPathRef:CoreGraphics框架。UIBezierPath:UIKit框架

    CGContextAddPath(ctx, path.CGPath);


    //4、把上下文内容显示到view上(渲染到View的layer)(stroke,fill)

    CGContextStrokePath(ctx);

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容