基本绘图 draw

#import "MXCircleView.h"

 

@implementation MXCircleView

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}

 

/**

 *  在view第一次显示到屏幕上的时候会调用一次

 */

- (void)drawRect:(CGRect)rect

{

    // 1.获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 2.画1/4圆

    CGContextMoveToPoint(ctx, 100, 100);

    CGContextAddLineToPoint(ctx, 100, 150);

    CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI, 1);

    CGContextClosePath(ctx);

    

    [[UIColor redColor] set];

    

    // 3.显示所绘制的东西

    CGContextFillPath(ctx);

}

 

/**

 *  画圆弧

 */

void drawArc()

{

    // 1.获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 2.画圆弧

    // x\y : 圆心

    // radius : 半径

    // startAngle : 开始角度

    // endAngle : 结束角度

    // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)

    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);

      // 3.显示所绘制的东西

    CGContextFillPath(ctx);

}

 

/**

 *  画圆

 */

void drawCircle()

{

    // 1.获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

     // 2.画圆

    CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));

   CGContextSetLineWidth(ctx, 10);

     // 3.显示所绘制的东西

    CGContextStrokePath(ctx);

}

 

/**

 *  画四边形

 */

void draw4Rect()

{

    // 1.获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 2.画矩形

    CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));

    

    // set : 同时设置为实心和空心颜色

    // setStroke : 设置空心颜色

    // setFill : 设置实心颜色

    [[UIColor whiteColor] set];

    

//    CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);

    

    // 3.绘制图形

    CGContextFillPath(ctx);

}

 

/**

 *  画三角形

 */

void drawTriangle()

{

    // 1.获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 2.画三角形

    CGContextMoveToPoint(ctx, 0, 0);

    CGContextAddLineToPoint(ctx, 100, 100);

    CGContextAddLineToPoint(ctx, 150, 80);

    // 关闭路径(连接起点和最后一个点)

    CGContextClosePath(ctx);

    

    //

    CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);

    

    // 3.绘制图形

    CGContextStrokePath(ctx);

}

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    // 1.获得图形上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 2.拼接图形(路径)

    // 设置线段宽度

    CGContextSetLineWidth(ctx, 10);

     // 设置线段头尾部的样式

    CGContextSetLineCap(ctx, kCGLineCapRound);

      // 设置线段转折点的样式

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    

    /**  第1根线段  **/

    // 设置颜色

    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);

    // 设置一个起点

    CGContextMoveToPoint(ctx, 10, 10);

    // 添加一条线段到(100, 100)

    CGContextAddLineToPoint(ctx, 100, 100);

    

    // 渲染一次

    CGContextStrokePath(ctx);

    

    

    /**  第2根线段  **/

    // 设置颜色

    CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);

    // 设置一个起点

    CGContextMoveToPoint(ctx, 200, 190);

    // 添加一条线段到(150, 40)

    CGContextAddLineToPoint(ctx, 150, 40);

    CGContextAddLineToPoint(ctx, 120, 60);

     // 3.渲染显示到view上面

    CGContextStrokePath(ctx);

}

 

 

@end

#import "MXTextImageView.h"

 

@implementation MXTextImageView

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}

 

- (void)drawRect:(CGRect)rect

{

    drawImage();

}

 

void drawImage()

{

    // 1.取得图片

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

    

    // 2.画

//    [image drawAtPoint:CGPointMake(50, 50)];

//    [image drawInRect:CGRectMake(0, 0, 150, 150)];

    [image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];

    

    // 3.画文字

    NSString *str = @"为xxx所画";

    [str drawInRect:CGRectMake(0, 180, 100, 30) withAttributes:nil];

}

 

/**

 *  画文字

 */

void drawText()

{

    // 1.获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 2.画矩形

    CGRect cubeRect = CGRectMake(50, 50, 100, 100);

    CGContextAddRect(ctx, cubeRect);

    // 3.显示所绘制的东西

    CGContextFillPath(ctx);

    

    

    

    // 4.画文字

    NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi";

    //    [str drawAtPoint:CGPointZero withAttributes:nil];

    

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

    // NSForegroundColorAttributeName : 文字颜色

    // NSFontAttributeName : 字体

    attrs[NSForegroundColorAttributeName] = [UIColor redColor];

    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:50];

    [str drawInRect:cubeRect withAttributes:attrs];

}

 

@end

 

posted @ 2016-03-29 21:25  lance.xiang  阅读(118)  评论(0编辑  收藏  举报