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

时间:2013-09-10 11:26:37

标签: ios objective-c line

enter image description here嗨,大家好,我正在用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;
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

#import <QuartzCore/QuartzCore.h>

然后,您可以为CAShapeLayer定义新的ivar:

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;
}