创建多个绘制的线条 [英] create multiple drawn lines

查看:83
本文介绍了创建多个绘制的线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要绘制2条不同的线条。通过另一个发布,我想出了如何重新绘制一条线。我的问题是,如果我想绘制2行,我需要有2个代码段来做吗?或者有没有办法画出n行?

so i need to draw 2 different lines. through another posting, i figured out how to redraw a line. my question is, if i want to draw 2 lines do i need to have 2 code segments to do it? or is there a way to draw n lines?

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSLog(@"drawRect called");
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
    CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
    CGContextStrokePath(context);
} 

实施

draw2D *myCustomView = [[draw2D alloc] init];

myCustomView.startPoint = CGPointMake(0, 0);
myCustomView.endPoint = CGPointMake(300, 300);
myCustomView.lineWidth = 5;
myCustomView.lineColor = [UIColor redColor];

myCustomView.frame = CGRectMake(0, 0, 500, 500);
[myCustomView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myCustomView];
[myCustomView setNeedsDisplay];

myCustomView.endPoint = CGPointMake(100, 100);

myCustomView.lineColor = [UIColor orangeColor];
[myCustomView setNeedsDisplay];

这只会重新划线。我会为我想要的每一行重复drawRect块吗?所以,如果我想要两行,我必须这样做:

this will just redraw the line. do i repeat the drawRect block for each line i want? so if i want two lines, do i have to do:

- (void)drawRect:(CGRect)rect
{
    //line1
    CGContextRef context1 = UIGraphicsGetCurrentContext();
    NSLog(@"drawRect called");
    CGContextSetLineWidth(context1, self.lineWidth1);
    CGContextSetStrokeColorWithColor(context1, self.lineColor1.CGColor);
    CGContextMoveToPoint(context1, self.startPoint1.x, self.startPoint1.y);
    CGContextAddLineToPoint(context1, self.endPoint1.x, self.endPoint1.y);
    CGContextStrokePath(context1);

    //line 2
    CGContextRef context2 = UIGraphicsGetCurrentContext();
    NSLog(@"drawRect called");
    CGContextSetLineWidth(context2, self.lineWidth2);
    CGContextSetStrokeColorWithColor(context2, self.lineColor2.CGColor);
    CGContextMoveToPoint(context2, self.startPoint2.x, self.startPoint2.y);
    CGContextAddLineToPoint(context2, self.endPoint2.x, self.endPoint2.y);
    CGContextStrokePath(context2);
} 

还是有更好的方法来处理我的线条?

or is there a better way to handle drawing my lines?

编辑:最终目标是将其与 UIView 一起使用,其中包含我用作表单的标签和文本框。我想要线条打破形式。

the end goal is to use it with a UIView with labels and textboxes i'm using as a form. i want lines to break up the form.

我想知道我是否应该只使用这些自定义draw2D UIView s作为行,并将它们置于顶部 UIView 表单,然后我可以创建多个draw2D UIViews

i'm wondering if i should just use these custom draw2D UIViews as lines only and place them on top of the UIView form, then i can just create multiple "draw2D" UIViews?

解决方案:

所以这里是我认为可行的代码:

so here is the code i figured out that would work:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
        [tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(10, 10)] forKey:@"start"];
        [tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"];

        [tempArray addObject:tempDict];

        NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc] init];
        [tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(400, 10)] forKey:@"start"];
        [tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"];

        [tempArray addObject:tempDict2];

        self.pointArray = [NSArray arrayWithArray:tempArray];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    for (int i = 0; i < [self.pointArray count]; i++)
    {
        CGPoint tempPoint = [[[self.pointArray objectAtIndex:i] objectForKey:@"start"] CGPointValue];
        CGPoint tempPoint2 = [[[self.pointArray objectAtIndex:i] objectForKey:@"end"] CGPointValue];

        CGContextMoveToPoint(context, tempPoint.x, tempPoint.y);
        CGContextAddLineToPoint(context, tempPoint2.x, tempPoint2.y);
    }

    CGContextStrokePath(context);
}

我在创建用于测试的数组时包含了我的initWithFrame:方法( CGPoint不是对象,因此必须弄清楚如何将它们包含在字典中。)

i included my initWithFrame: method on creating the array that i used for testing (CGPoint's are not objects, so had to figure out how to include them in an dictionary).

所以这将循环并创建n行。

so this will loop through and create n lines.

推荐答案

您可以根据需要多次重复此位(每次显示不同的坐标)。

You can just repeat this bit as many times as you need (with different coordinates each time obviously).

CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);

第一行代码设置行的起点,第二行代码绘制它。你不需要重复所有剩下的代码。

The first line of code sets the start point of the line, the second line of code draws it. You don't need to repeat all the rest of the code.

换句话说,你可以画出这样的多行:

In other words, you can draw multiple lines like this:

//set up context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, self.lineWidth1);
CGContextSetStrokeColorWithColor(context, self.lineColor1.CGColor);

//line1
CGContextMoveToPoint(context, self.startPoint1.x, self.startPoint1.y);
CGContextAddLineToPoint(context, self.endPoint1.x, self.endPoint1.y);

//line 2
CGContextMoveToPoint(context, self.startPoint2.x, self.startPoint2.y);
CGContextAddLineToPoint(context, self.endPoint2.x, self.endPoint2.y);

//line 3
etc...

//finished drawing
CGContextStrokePath(context);

每次画线时都不需要复制整批,你可以重复一遍这两行代码。

You don't need to copy the whole lot each time you draw a line, you can just repeat those two lines of code.

这篇关于创建多个绘制的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆