我正在尝试在用户每次点击时连接线路。在用户点击的点被创建,但是连接它们的线却没有。它给了我以下错误:

CGContextAddLineToPoint: no current point.


谁能告诉我为什么?我检查了SO,但没有找到任何相关内容,因为我的笔触方法才刚刚结束。这是我正在尝试的:

  - (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:NO];
        [self setBackgroundColor:[UIColor clearColor]];
        o = 0.5 / 1.0;
        sW = 5.0;
        r = 255.0/255.0;
        g = 59.0/255.0;
        b = 48.0/255.0;
        first = CGPointZero;
        second = CGPointZero;
        third = CGPointZero;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self setOpacity:o];
    if (CGPointEqualToPoint(first, CGPointZero)) {
        first = [touch locationInView:self];
    }
    else if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
        second = [touch locationInView:self];
    }
    else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
        third = [touch locationInView:self];
    }
    else {
        first = CGPointZero;
        second = CGPointZero;
        third = CGPointZero;
    }

    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
        first = [touch locationInView:self];
    }
    else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
        second = [touch locationInView:self];
    }
    else {
        third = [touch locationInView:self];
    }

    [self setNeedsDisplay];


}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self drawBitmap];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (void)drawRect:(CGRect)rect {
    [incrementalImage drawInRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, sW);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
    NSLog(@"R:%f, G:%f, B:%f",r,g,b);

    if (!CGPointEqualToPoint(first, CGPointZero)) {
        CGRect rectangle = CGRectMake(first.x, first.y, 20, 20);
        CGContextAddEllipseInRect(context, rectangle);
        CGContextFillEllipseInRect(context, rectangle);
        CGContextMoveToPoint(context, first.x, first.y);
    }

    if (!CGPointEqualToPoint(second, CGPointZero)) {
        CGRect rectangle2 = CGRectMake(second.x, second.y, 20, 20);
        CGContextAddEllipseInRect(context, rectangle2);
        CGContextFillEllipseInRect(context, rectangle2);
        CGContextAddLineToPoint(context, second.x, second.y);
    }

    if (!CGPointEqualToPoint(third, CGPointZero)) {
        CGRect rectangle3 = CGRectMake(third.x, third.y, 20, 20);
        CGContextAddEllipseInRect(context, rectangle3);
        CGContextFillEllipseInRect(context, rectangle3);
        CGContextAddLineToPoint(context, third.x, third.y);
    }

    CGContextStrokePath(context);
    CGContextDrawPath(context, kCGPathFill);
}

- (void)drawBitmap {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    [[UIColor redColor] setStroke];
    //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 255.0/255.0, 255.0/255.0, 255.0/255.0, 1.0/1.0);
    if (!incrementalImage) { // first draw;
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor clearColor] setFill];
        [rectpath fill]; // fill it
    }
    [incrementalImage drawAtPoint:CGPointZero];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, sW);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);

    if (!CGPointEqualToPoint(first, CGPointZero)) {
        CGRect rectangle = CGRectMake(first.x, first.y, 20, 20);
        CGContextAddEllipseInRect(context, rectangle);
        CGContextFillEllipseInRect(context, rectangle);
        CGContextMoveToPoint(context, first.x, first.y);
    }

    if (!CGPointEqualToPoint(second, CGPointZero)) {
        CGRect rectangle2 = CGRectMake(second.x, second.y, 20, 20);
        CGContextAddEllipseInRect(context, rectangle2);
        CGContextFillEllipseInRect(context, rectangle2);
        CGContextAddLineToPoint(context, second.x, second.y);
    }

    if (!CGPointEqualToPoint(third, CGPointZero)) {
        CGRect rectangle3 = CGRectMake(third.x, third.y, 20, 20);
        CGContextAddEllipseInRect(context, rectangle3);
        CGContextFillEllipseInRect(context, rectangle3);
        CGContextAddLineToPoint(context, third.x, third.y);
    }
    CGContextStrokePath(context);

    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}


此外,笔触颜色始终为黑色。我nslog它,并指出正确的颜色。没有当前点错误没有任何意义,是因为我使用相同的参数在水龙头上创建圆并且效果很好,唯一无效的是直线。

最佳答案

在这里,您要求提出第三点,但应该第二点。

if(!CGPointEqualToPoint(third, CGPointZero)){
CGContextAddLineToPoint(context, second.x, second.y);
}

CGContextAddEllipseInRect(context, rectangle3);
CGContextFillEllipseInRect(context, rectangle3);


在这里,您再次呼吁第三点,但在适当的位置

if(!CGPointEqualToPoint(third, CGPointZero)){
CGContextAddLineToPoint(context, third.x, third.y);
}

关于ios - 无法绘制线条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20206493/

10-11 00:15