ios基本线条绘制

2018-11-26  本文已影响0人  会写bug的程序媛

/*

 作用:专门用来绘图

 什么时候调用:系统自动调用,当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);

}

上一篇 下一篇

猜你喜欢

热点阅读