CGContextRef 绘图

如何绘制一个矩形

添加一个属性

@property(nonatomic,strong) NSString* RectNumber;

1. 首先重写UIview的drawRect方法

 - (void)drawRect:(CGRect)rect {
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    int number = [self.RectNumber intValue];
    int x = 50;
    int y = 0;
    
    CGContextBeginPath(ctx);
    //设置图形间距
    for (int i=0; i<number; i++) {
        if (i%3 == 0) {
            y+=100;
            x=50;
        }
        [self AddRect:ctx leftPoint:x rightPoint:y 90 height:50];
        x+=100;
    }
    
    CGContextSetRGBStrokeColor(ctx, 0.5, 0, 0, 1);
    CGContextSetRGBFillColor(ctx, 0, 1, 0, 1);
    CGContextDrawPath(ctx, kCGPathEOFillStroke);
    //打开路径
    CGContextStrokePath(ctx);
    //关闭路径
    CGContextClosePath(ctx);
    

}

2. 实现自定义方法

-(void)AddRect:(CGContextRef)r leftPoint:(CGFloat)x1 rightPoint:(CGFloat)x2 (CGFloat)w height:(CGFloat)h
{
    CGContextMoveToPoint(r, x1, x2);
    //添加一条连接到右上角的线
    CGContextAddLineToPoint(r, x1+w, x2);
    //添加一条连接到右下角的先
    CGContextAddLineToPoint(r, x1+w, x2+h);
    //添加一条连接到左下角的线
    CGContextAddLineToPoint(r, x1, x2+h);
    //添加一条连接到左上角的线
    CGContextAddLineToPoint(r, x1, x2);
}

3添加一个生成按钮

    field2 = [[UITextField alloc]initWithFrame:CGRectMake(50, 150, 200, 40)];
    field2.placeholder = @"输入要多少个矩形";
    [self.view addSubview:field2];
    
    btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(100, 200, 80, 40);
    [btn2 setTitle:@"生成" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(click3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    self.view.backgroundColor = [UIColor whiteColor];

4 viewController 里实现生产方法

    curRect = [[CurRectView alloc]initWithFrame:self.view.frame];
    curRect.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click4)];
    [curRect addGestureRecognizer:tap];
    curRect.RectNumber = field2.text;
    [self.view addSubview:curRect];

5 手势移除视图
    [curRect removeFromSuperview];

如何绘制五角星

添加一个属性

@property (nonatomic,strong) NSString *starNumber;

1. 首先重写UIview的drawRect方法

-(void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    int number = [self.starNumber intValue];
    int x = 50;
    int y = 0;
    
    CGContextBeginPath(ctx);
    //设置图像间距
    for (int i=0; i<number; i++) {
        if (i%4 == 0) {
            y+=100;
            x=50;
        }
        [self CGContextAddStar:ctx starCount:5 starX:x starY:y starSize:50];
        x+=100;
    }
    //图形边框颜色
    CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
    //设置填充颜色
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
    CGContextDrawPath(ctx, kCGPathEOFillStroke);

    //打开路径
    CGContextStrokePath(ctx);
    //关闭路径
    CGContextClosePath(ctx);
}

2. 实现自定义方法

-(void)CGContextAddStar:(CGContextRef)c starCount:(NSInteger)n starX:(CGFloat)dx starY:(CGFloat)yx starSize:(NSInteger)size
{
    //m_pi 是圆周率
    CGFloat dig = 4*M_PI/n;
    //移动到制定点
    CGContextMoveToPoint(c, dx, yx+size);
    for (int i = 1; i<=n; i++) {
        CGFloat x = sin(i*dig);
        CGFloat y = cos(i*dig);
        //绘制从当前点到制定点的线条
        CGContextAddLineToPoint(c, x*size+dx, y*size+yx);
        
    }
}

3添加一个生成按钮

    field1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 40)];
    field1.placeholder = @"输入要多少个五角星";
    [self.view addSubview:field1];
    
    btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(100, 100, 80, 40);
    [btn1 setTitle:@"生成" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    self.view.backgroundColor = [UIColor whiteColor];

4 viewController 里实现生产方法

    cur = [[CurView alloc]initWithFrame:self.view.frame];
    cur.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click2)];
    [cur addGestureRecognizer:tap];
    cur.starNumber = field1.text;
    [self.view addSubview:cur];

5 手势移除视图

    [cur removeFromSuperview];

注 :viewcontroller 里添加全局变量

    UITextField *field1,*field2;
    UIButton *btn1,*btn2;
    CurView *cur;
    CurRectView *curRect;