如何在两个CGpoint之间擦除一条线?

3

在这里输入图片描述

我正在创建一条线,当用户触摸屏幕并拖动手指时。问题是,在(touchMoved:)中每个点上都创建了一条线。但最后应该只有一条线而不是多条。如何在创建新的线后擦除或删除上一条线?以下是我的代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if(ArrowDraw==YES){
        NSLog(@"ArrowDrawing");
        if ([[event allTouches]count]==1){
            UITouch *touch = [touches anyObject];
            CGPoint currentPoint = [touch locationInView:FullImageView];
            UIGraphicsBeginImageContext(self.view.frame.size);
            [self.tempDrawImage.image drawInRect:CGRectMake(0, 0,      self.view.frame.size.width, self.view.frame.size.height)];
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), firstPoint.x, firstPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1 );
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
            [self.tempDrawImage setAlpha:opacity];
            UIGraphicsEndImageContext();
            //firstPoint = currentPoint;
            NSLog(@"TouchMoving x=%f y=%f",firstPoint.x,firstPoint.y);
        }
        UIGraphicsBeginImageContext(self.FullImageView.frame.size);
        [self.FullImageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
        self.FullImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        self.tempDrawImage.hidden=YES;
    }
}

可能是重复的问题,参考链接:http://stackoverflow.com/questions/10601691/how-to-remove-line-in-core-graphics - Puneet Sharma
我喜欢假设,你只画“一条”线。通过手势如平移或捏合来移动或删除此线。 - Thomas
@thomas 我想创建一条可移动和可拖动到最后一个点位置的线。但是每次都会创建新的线,我找不到任何解决办法来擦除上一个线条。 - Mohit Tomar
1个回答

2
关键是不要更新图片,而是在图像上方绘制箭头。然后随着touchesMoved的到来,用另一个箭头替换它。
例如,我可以通过将QuartzCore.framework添加到目标的“链接二进制文件与库”中,并在.m文件的开头添加以下行来实现:
#import <QuartzCore/QuartzCore.h>

然后你可以为你的CAShapeLayer定义一个新的实例变量:

CAShapeLayer *shapeLayer;

最后,更新你的touchesMoved方法如下:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (ArrowDraw==YES){
        if ([[event allTouches]count]==1) {
            UITouch *touch = [touches anyObject];
            CGPoint currentPoint = [touch locationInView:FullImageView];

            if (!shapeLayer)
            {
                shapeLayer = [CAShapeLayer layer];
                shapeLayer.lineWidth = 1;
                shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
                shapeLayer.fillColor = [[UIColor clearColor] CGColor];
                [FullImageView.layer addSublayer:shapeLayer];
            }
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:firstPoint];
            [path addLineToPoint:currentPoint];
            shapeLayer.path = [path CGPath];
        }
    }
}

如果需要,您可以修改那个UIBezierPath以包含箭头,例如:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (ArrowDraw==YES){
        if ([[event allTouches]count]==1) {
            UITouch *touch = [touches anyObject];
            CGPoint currentPoint = [touch locationInView:FullImageView];

            if (!shapeLayer) {
                [self createShapeLayer];
            }

            shapeLayer.path = [[self arrowPathFrom:firstPoint to:currentPoint arrowheadSize:10.0] CGPath];
        }
    }
}

- (void)createShapeLayer
{
    shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineWidth = 1;
    shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
    shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    [FullImageView.layer addSublayer:shapeLayer];
}

- (UIBezierPath *)arrowPathFrom:(CGPoint)start to:(CGPoint)end arrowheadSize:(CGFloat)arrowheadSize
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:start];
    [path addLineToPoint:end];

    // add arrowhead

    CGFloat angle = atan2(end.y - start.y, end.x - start.x) + M_PI * 3.0 / 4.0;
    [path addLineToPoint:CGPointMake(cos(angle) * arrowheadSize + end.x, sin(angle) * arrowheadSize + end.y)];
    [path addLineToPoint:end];
    angle = atan2(end.y - start.y, end.x - start.x) - M_PI * 3.0 / 4.0;
    [path addLineToPoint:CGPointMake(cos(angle) * arrowheadSize + end.x, sin(angle) * arrowheadSize + end.y)];
    [path addLineToPoint:end];

    return path;
}

它的工作正常,但只创建了一个箭头。这意味着当我创建一个箭头并想添加另一个箭头时,它会删除第一个箭头并创建另一个箭头。 - Mohit Tomar
1
@Mohittomar 在 touchedEnded 中你可以将 shapeLayer 变量设置为 nil。这样就可以绘制多条线了。你还可以在模型中保存箭头的起点和终点(但这与你的问题无关)。 - Rob
1
我由衷地向你道歉,兄弟。 - Mohit Tomar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接