Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SunnyLi

Guest
Original poster
Oct 7, 2010
22
0
Hi Guys,

At the minute, i can draw a line between to dots fine. But when i start drawing the second line between other dots the previous line disappears. Ive been working on the drawRect and touchesbegan methods but i cant find anything to help me.

This is the code;

Code:
-(void) drawRect:(CGRect)rect

{
	if(!mdelegate){
	mdelegate = (Dot2DotAppDelegate*)[[UIApplication sharedApplication]delegate];}

	if(!mdelegate.pointarray){
		mdelegate.pointarray=[[NSMutableArray alloc] init]; }
	

	CGContextRef ctx = UIGraphicsGetCurrentContext();
	
	if([mdelegate.pointarray count])
	{
		CGContextRef ctx = UIGraphicsGetCurrentContext();
		CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
		CGContextSetLineWidth(ctx, 6.0);
		
		float firstpointx= [[mdelegate.pointarray objectAtIndex:0] floatValue];
		float firstpointy= [[mdelegate.pointarray objectAtIndex:1]floatValue];
		float secondpointx= [[mdelegate.pointarray objectAtIndex:2]floatValue];
		float secondpointy= [[mdelegate.pointarray objectAtIndex:3]floatValue];
		
		CGContextMoveToPoint(ctx,firstpointx,firstpointy);///move to ur first dot
		
		CGContextAddLineToPoint(ctx, secondpointx, secondpointy);//add line from first dot to second dot
		CGContextSetLineCap(ctx, kCGLineCapRound);
		CGContextStrokePath(ctx);
		CGContextSetLineJoin(ctx, kCGLineJoinRound);
		
	}	
	
	CGContextRef contextRef = UIGraphicsGetCurrentContext();
	CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1);
	CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5);
	
		
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(100, 100, 25, 25));
	
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(100, 200, 25, 25));
	
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(100, 300, 25, 25));
	
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(10, 200, 25, 25));
	
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(200, 200, 25, 25));
	
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(200, 20, 25, 25));
	
	// Draw a green solid circle
	CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
	CGContextFillEllipseInRect(ctx, CGRectMake(290, 300, 25, 25));

 }

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	//NSLog(@"touched");
	CGPoint curPoint = [[touches anyObject] locationInView:self];
	[mdelegate.pointarray addObject:[NSNumber numberWithFloat:curPoint.x]];
	[mdelegate.pointarray addObject:[NSNumber numberWithFloat:curPoint.y]];
	
	[self setNeedsDisplay]; // calls drawRectMethod
}

Can anyone help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.