Quartz 2D绘图基础:CGContextRef

来源:互联网 发布:三坐标测量软件 编辑:程序博客网 时间:2024/05/17 21:57

利用storyboard,自定义一个继承自UIView的类,在属性面板中重定义主视图的类。在自定义的类中重写drawRect:方法即可。


//

//  WBGeometryView.m

//  1119绘制集合图形

//

//  Created by weibiao on 15/11/19.

//  Copyright © 2015 weibiao. All rights reserved.

//


#import "WBGeometryView.h"


@implementation WBGeometryView


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

 */

- (void)drawRect:(CGRect)rect {

    // Drawing code

    CGContextRef ctx =UIGraphicsGetCurrentContext();//获取绘图上下文

    CGContextSetLineWidth(ctx,16);//设置线宽

    CGContextSetRGBStrokeColor(ctx,0, 1,0, 1);//设置线的颜色

    

    // 下面绘制三个线的端点测试绘制情况

    // 定义4个点,绘制线段

    constCGPoint points[] = {CGPointMake(10,20),CGPointMake(100,20),CGPointMake(100,20),CGPointMake(20,50)};

    CGContextStrokeLineSegments(ctx, points,4);//绘制线段,默认不绘制端点

    CGContextSetLineCap(ctx,kCGLineCapSquare);//方形端点

    

    const CGPoint points1[] = {

        CGPointMake(110,20),CGPointMake(200,20),CGPointMake(200,20),CGPointMake(120,50)

    };

    CGContextStrokeLineSegments(ctx, points1,4);

    CGContextSetLineCap(ctx,kCGLineCapRound);

    

    constCGPoint points2[] = {CGPointMake(210,20),CGPointMake(300,20),CGPointMake(300,20),CGPointMake(220,50)};

    CGContextStrokeLineSegments(ctx, points2,4);

    CGContextSetLineCap(ctx,kCGLineCapButt);

    

    // 绘制线段测试电线模式

    CGContextSetLineWidth(ctx,10);//设置线宽

    CGFloat patterns1[] = {6,10};

    CGContextSetLineDash(ctx, 0, patterns1, 1);//设置线段的点线模式,实线宽6,间距为10

    const CGPoint points3[]  = {CGPointMake(40,65),CGPointMake(280,65)};// 定义两个点,

    CGContextStrokeLineSegments(ctx, points3,2);//绘制线段

     // 设置点线模式:实线宽6,间距宽10,但第一个实线宽为3

    CGContextSetLineDash(ctx, 3, patterns1, 1);

    

    // 定义两个点,绘制线段

    const CGPoint points4[] = {CGPointMake(40,85),CGPointMake(280,85)};

    CGContextStrokeLineSegments(ctx, points4,2);//绘制线段

    

    CGFloat patterns2[] = {5,1,4,1,3,1,2,1,1,1,1,2,1,3,1,4,1,5};

    CGContextSetLineDash(ctx, 0, patterns2, 18);

    

    const CGPoint points5[] = {CGPointMake(40,105),CGPointMake(280,105)};

    CGContextStrokeLineSegments(ctx, points5,2);

    

    // ----下面填充矩形

    CGContextSetStrokeColorWithColor(ctx, [UIColorblueColor].CGColor);

    CGContextSetLineWidth(ctx,14);

    CGContextSetFillColorWithColor(ctx, [UIColorredColor].CGColor);

    CGContextFillRect(ctx,CGRectMake(30,120, 120, 60));

    CGContextSetFillColorWithColor(ctx, [UIColoryellowColor].CGColor);

    CGContextFillRect(ctx,CGRectMake(80,160, 120, 60));

    

    // 绘制矩形边框

    CGContextSetLineDash(ctx,0, 0,0);//取消设置点线模式

    CGContextStrokeRect(ctx,CGRectMake(30,230, 120, 60));//绘制一个矩形边框

    CGContextSetStrokeColorWithColor(ctx, [UIColorpurpleColor].CGColor);//设置线条颜色

    CGContextSetLineJoin(ctx,kCGLineJoinRound);//设置线条连接点的形状

    CGContextStrokeRect(ctx,CGRectMake(80,260, 120, 60));

    CGContextSetRGBStrokeColor(ctx,1.0, 0, 1.0,1.0);//设置线条颜色

    CGContextSetLineJoin(ctx,kCGLineJoinBevel);//设置连接点形状

    

    CGContextStrokeRect(ctx,CGRectMake(130,290, 120, 60));//绘制一个矩形边框

    // 设置线条颜色

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

    

    

    // 下面绘制和填充一个椭圆

    CGContextStrokeEllipseInRect(ctx,CGRectMake(30,380, 120, 60));//绘制一个椭圆

    CGContextSetRGBFillColor(ctx,1, 0,1, 1);//设置填充颜色

    CGContextFillEllipseInRect(ctx,CGRectMake(180,380, 120, 60));

    

}



@end



0 0
原创粉丝点击