3

在此处输入图像描述

当用户触摸屏幕并拖动手指时,我正在 ipad 中创建一条线。问题是在我们拖动它的 (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;
    }
}
4

1 回答 1

2

关键是不要更新图像,而只是在图像顶部绘制箭头。touchesMoved然后在进来时用另一个箭头替换箭头。

例如,我可以通过将 QuartzCore.framework 添加到目标的“Link Binary With Libraries”来使用 QuartzCore.framework,并将以下行添加到 .m 的开头:

#import <QuartzCore/QuartzCore.h>

然后你可以为你定义一个新的 ivar 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;
}
于 2013-09-10T12:41:18.013 回答