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);
}
implementation
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];
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?
EDIT: 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.
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?
SOLUTION:
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);
}
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).
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).
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:
You don’t need to copy the whole lot each time you draw a line, you can just repeat those two lines of code.