Quartz2D画图

来源:互联网 发布:上海浦东机场有没有mac 编辑:程序博客网 时间:2024/05/18 01:16
创建一个继承自UIView的类,在其

- (void)drawRect:(CGRect)rect;方法中画图


实例如下:

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    //用点和线画三角形和正方形

//    drawRectAndTriangle();

    //画矩形

//    draw4Rect();

    //画原园

//    drawCircle();

    //画弧线

    drawARC();

    

}


/**

 *  把图片画到view上去

 */

void drawImage()

{

    UIImage *img = [UIImage imageNamed:@"xcode"];

    //在矩形中画image,图片根据矩形的宽高而拉伸

   [img drawInRect:CGRectMake(10, 10, 230, 230)];

    

    UIImage *img2 = [UIImage imageNamed:@"xcode2"];

    //根据图片的大小平铺整个Rect

    [img2 drawAsPatternInRect:CGRectMake(1010220220)];


}


/*

*画文字

*/

void drawStr()

{

    //直接把文字画在view上,不需要上下文

    NSString *str = @"哈哈";

    NSMutableDictionary *strdict = [NSMutableDictionary dictionary];

    strdict[NSFontAttributeName] = [UIFont systemFontOfSize:17];//字体大小

    strdict[NSForegroundColorAttributeName] = [UIColor greenColor];//字体颜色

//    [str drawAtPoint:CGPointMake(10, 10) withAttributes:strdict];

    

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGRect r = CGRectMake(5050100100);

    CGContextAddRect(ctx, r);

    CGContextFillPath(ctx);

    //把文字写入到矩形里

    [str drawInRect:r withAttributes:strdict];

    

    

}


/**

 *  画弧线

 */

void drawARC()

{

    //获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    CGContextSetRGBStrokeColor(ctx, 0.40.40.21);

    CGContextSetLineWidth(ctx, 3);

    //x/y:圆心坐标

    //radius:半径

    //startAngle:开始弧度

    //endAge:结尾弧度

    //clockwise:顺逆失真(0:顺时针 1:逆时针)

    CGContextAddArc(ctx, 70100300M_PI1);

    //渲染

    CGContextStrokePath(ctx);

    CGContextAddArc(ctx, 150100300M_PI1);

    CGContextStrokePath(ctx);

    

    CGContextAddArc(ctx, 110130500M_PI0);

    CGContextStrokePath(ctx);

    

    //1/4

    CGContextMoveToPoint(ctx, 250130);

    CGContextAddLineToPoint(ctx, 250200);

    //画圆弧线(顺时针从二分之ππ

    CGContextAddArc(ctx, 25013070M_PI_2M_PI0);

    //闭线

    CGContextClosePath(ctx);

    CGContextStrokePath(ctx);

    

}

/**

 *  画原型

 */

void drawCircle()

{

    //获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //设置线条宽度

    CGContextSetLineWidth(ctx, 5);

    //设置线条颜色

    CGContextSetRGBStrokeColor(ctx, 0.40.50.11);

    //画一个坐标为5010,宽50,高40的椭圆

    CGContextAddEllipseInRect(ctx, CGRectMake(50905040));

    CGContextAddEllipseInRect(ctx, CGRectMake(90905040));

    CGContextAddEllipseInRect(ctx, CGRectMake(130905040));

    CGContextAddEllipseInRect(ctx, CGRectMake(170905040));

    CGContextAddEllipseInRect(ctx, CGRectMake(210905040));

    CGContextStrokePath(ctx);


}


/**

 *  画矩形

 */

void draw4Rect()

{

    //获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

//    CGContextSetRGBFillColor(ctx, 0.3, 0.4, 0.3, 0.8);//设置填充颜色

    //set:设置实心和空心的颜色

    [[UIColor greenColor]set];

    //setFill:设置实心的颜色

    [[UIColor greenColorsetFill];

    //setStroke:设置实心的颜色

    [[UIColor greenColorsetStroke];

    //画一个100x200的矩形

    CGContextAddRect(ctx, CGRectMake(1010100200));

    //渲染(实心图形)

    CGContextFillPath(ctx);

}

/**

 *  用点和线画三角形和正方形

 */

void drawRectAndTriangle()

{

    //获得上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //设置头尾部的样式

    CGContextSetLineCap(ctx, kCGLineCapRound);//设置线头尾部都为原型

    //设置转折点样式

    CGContextSetLineJoin(ctx, kCGLineJoinRound);//设置线的转折点为原型

    //设置线的宽度

    CGContextSetLineWidth(ctx, 10);

    //画正方形

    //设置线的颜色

    CGContextSetRGBStrokeColor(ctx, 1001);//红色

    //画一个点

    CGContextMoveToPoint(ctx, 30,30);

    //画一条线

    CGContextAddLineToPoint(ctx, 30120);

    CGContextAddLineToPoint(ctx, 120120);

    CGContextAddLineToPoint(ctx, 12030);

    CGContextAddLineToPoint(ctx, 3030);

    //渲染显示到view上面(实心图形)

    CGContextStrokePath(ctx);

    /**

     *  画三角形

     */

    //设置线的颜色

    CGContextSetRGBStrokeColor(ctx, 0101);//绿色

    //画一个点

    CGContextMoveToPoint(ctx, 130,130);

    //画一条线

    CGContextAddLineToPoint(ctx, 130200);

    CGContextAddLineToPoint(ctx, 200130);

    //    CGContextAddLineToPoint(ctx, 130, 130);

    CGContextClosePath(ctx);//封闭

    //渲染显示到view上面(空心图形)

    CGContextStrokePath(ctx);


}


0 0
原创粉丝点击