ios - 如何删除两个 CGpoint 之间的线?

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

最佳答案

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

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

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

关于ios - 如何删除两个 CGpoint 之间的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717715/

相关文章:

ios - Swift 4 中的布局 anchor

每次用户进入后台至少 30 秒时,在 iOS 13 上运行的 iOS 应用程序都会重置

iphone - 添加依赖于scrollView.contentSize的 subview 的正确位置是什么?

javascript - Chartjs 柱上平均线

android - achartengine - 不同点和线的不同颜色

matlab - 当我添加新输入时,如何使以前的输入在 Matlab 图中逐渐淡出

ios - 检查 UIImageView 中的图像不是占位符图像并附加到 UIImage 数组

ios - 使 UIButton 在 TableView header 中可访问(可访问性)

objective-c - 有什么方法可以标记一个 c 函数或 obj-c 方法以在运行时识别它吗?

iphone - ASIHTTP请求问题