不能drawRect线

时间:2013-11-26 00:46:12

标签: ios objective-c cocoa-touch uiview drawrect

我正在尝试在用户进行的每次点击时连接线条。创建用户点击的点,但连接它们的线不是。它给了我以下错误:

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它,它说明正确的颜色。对于没有当前点错误没有意义的事实是,我使用相同的参数在水龙头上创建圆圈并且它工作正常,唯一不起作用的是线。

1 个答案:

答案 0 :(得分:1)

在这里你要求第三点,但它应该是第二点。

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