我可以使用touchesbegan中的代码在我的图像上绘制点。我将我的坐标存储在NSMutable数组中。我希望它画线,因为我在屏幕上标记点..但是我的drawRect不会发射我猜...你可以告诉我该做什么..
-(void) drawRect:(CGRect)rect
{
int *count = 0;
if([pointarray count]!=0)
{
float firstpointx= [[pointarray objectAtIndex:0]floatValue];
float firstpointy= [[pointarray objectAtIndex:1]floatValue];
float secondpointx= [[pointarray objectAtIndex:2]floatValue];
float secondpointy= [[pointarray objectAtIndex:3]floatValue];
//NSMutableArray *coordinates = [[NSMutableArray alloc] init];
for (int i=0; i<=[pointarray count]; i++) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetLineWidth(ctx, 2.0);
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);
[pointarray removeAllObjects];//remove first two points from ur array so that next line is not drawn in continuous with previous line
}
count++;
}
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
pointarray=[[NSMutableArray alloc]init];
CGPoint curPoint = [[touches anyObject] locationInView:self.map];
[pointarray addObject:[NSNumber numberWithFloat:curPoint.x]];
[pointarray addObject:[NSNumber numberWithFloat:curPoint.y]];
[_map setNeedsDisplay];
[self logMessage:[NSString stringWithFormat:@"Sending : %@", pointarray]];
NSLog(@"the point array is %@",pointarray);
NSArray *coordinates = [[NSArray alloc]initWithArray:pointarray copyItems:YES];
NSLog(@"the coordinate array %@",coordinates);
//[self.map setNeedsDisplay]; // calls drawRectMethod
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(curPoint.x, curPoint.y, 10, 10)];
view.backgroundColor = [UIColor redColor];
[self.map addSubview:view];
}
答案 0 :(得分:1)
您应该从此方法中调用setNeedsDisplay
:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
答案 1 :(得分:1)
在touchedBegan:withEvent:
上,您只需在setNeedsDisplay
上呼叫_map
,而不是self
,这就是视图无法重绘的原因。
此外,您只需添加两个点,但在drawRect
中,您可以编码,如果它确定数组包含四个点。您可能想要做的是在touchesEnded:withEvent
中添加两个点?如果是这样,您应该从那里拨打setNeedsDisplay
。