0

私のアプリでは、ユーザーが地図をタッチして移動しているときに、2本の線を描きたいと思っています。これどうやってするの。以下のコードを使用して、1本の線を描画しています。 コード:

    - (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);を追加しました。2番目の線を引くための線。さて、このコードで何が間違っていたのか。私を助けてください。問題は、地図上で指を動かしているときに、別のポイントから1つの現在のポイントに2本の線を描きたいということです。よろしくお願いします。

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 に答える