在我的应用程序中我想做的同时用户触摸并在地图上移动我想画几行。我怎样才能做到这一点。我使用下面的代码绘制一行。 的码
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:drawImage];
currentPoint.y -= 20;
mapView.scrollEnabled = NO;
lastpinpoint1.x = 170.000000;
lastpinpoint1.y = 327.000000;
UIGraphicsBeginImageContext(drawImage.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[imgMap drawInRect:CGRectMake(0, 0, drawImage.frame.size.width,drawImage.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 2.0);
// CGContextSetRGBStrokeColor(ctx, 0.0, 0.5, 0.6, 1.0);
CGContextSetStrokeColorWithColor(ctx, [[UIColor redColor] CGColor]);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, lastpinpoint.x, lastpinpoint.y);
CGContextMoveToPoint(ctx, lastpinpoint1.x, lastpinpoint1.y);
CGContextAddLineToPoint(ctx, currentPoint.x, currentPoint.y);
CGContextStrokePath(ctx);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
在上面的代码中没有这个 CGContextMoveToPoint(ctx,lastpinpoint1.x,lastpinpoint1.y); 这一行用于绘制单行。但我添加了 CGContextMoveToPoint(ctx,lastpinpoint1.x,lastpinpoint1.y); 行来绘制第二行。现在我在这段代码中做错了什么。请帮我。 问题是:当我在地图上移动手指时我想从不同点到一个当前点绘制几条线。谢谢你的进步。
答案 0 :(得分:1)
试试这个答案
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0 , 0.0, 1.0, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
[self setAlpha:1.0];
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(self.frame.size);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x+20, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x+20, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0 , 0.0, 1.0, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
[self setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
希望它有所帮助!!!