0

在我的应用程序中,当用户触摸并在地图上移动时,我想画几条线。我怎样才能做到这一点。我正在使用下面的代码来绘制一条线。 代码:

    - (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); line 绘制第二条线。现在我在这段代码中做错了什么。请帮我。问题是:当我在地图上移动手指时,我想从不同的点画几条线到一个当前点。感谢提前。

4

1 回答 1

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;

}

希望能帮助到你!!!

于 2013-03-06T07:19:46.150 回答