美文网首页
绘制图形代码

绘制图形代码

作者: 小破孩丫 | 来源:发表于2016-01-12 13:11 被阅读72次

    画线

    - (void)drawRect:(CGRect)rect{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(context, 10, 10);

    CGContextAddLineToPoint(context, 30, 100);

    CGContextStrokePath(context);

    }

    画三角形

    - (void)drawRect:(CGRect)rect{

    //画三角形

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1);

    CGContextMoveToPoint(context, 10, 10);

    CGContextAddLineToPoint(context, 110, 10);

    CGContextAddLineToPoint(context, 110, 110);

    CGContextClosePath(context);

    CGContextStrokePath(context);

    }

    画矩形

    - (void)drawRect:(CGRect)rect{

    //画矩形

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddRect(context, CGRectMake(10, 20, 100, 100));

    //CGContextFillPath(context);

    CGContextStrokePath(context);

    }

    画扇形

    - (void)drawRect:(CGRect)rect{

    //画扇形

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(context, 100, 100);

    CGContextAddArc(context, 100, 100,60, - 3 * M_PI_4, -M_PI_4, 1);

    CGContextClosePath(context);

    CGContextStrokePath(context);

    }

    画弧

    -(void)drawArc{

    CGContextRef context = UIGraphicsGetCurrentContext();

    //x,y 圆心

    //radius 半径

    //startAngle 画弧的起始位置

    //endAngel 画弧的结束位置

    //clockwise 0 顺针 1 逆时针

    CGContextAddArc(context, 100, 100, 60, 0, M_PI, 1);

    CGContextClosePath(context);

    //渲染

    CGContextStrokePath(context);

    //CGContextFillPath(context);

    }

    画圆

    - (void)drawRect:(CGRect)rect{

    //画圆

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddEllipseInRect(context, CGRectMake(10, 10, 100, 100));

    CGContextStrokePath(context);

    }

    画图片

    - (void)drawRect:(CGRect)rect{

    //画图片

    UIImage *image = [UIImage imageNamed:@"papa"];

    [image drawAsPatternInRect:CGRectMake(10, 10, 50, 50)];

    }

    画文字

    - (void)drawRect:(CGRect)rect{

    //画文字

    NSString *str = @"啦啦啦啦啦啦啦啦啦啦啦啦";

    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor yellowColor]};

    [str drawInRect:CGRectMake(10, 10, 100, 100) withAttributes:attr];

    }

    相关文章

      网友评论

          本文标题:绘制图形代码

          本文链接:https://www.haomeiwen.com/subject/qikphttx.html