首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Can't drawRect line

Can't drawRect line
EN

Stack Overflow用户
提问于 2013-11-26 08:46:12
回答 1查看 126关注 0票数 0

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

代码语言:javascript
运行
复制
CGContextAddLineToPoint: no current point.

有人能告诉我为什么吗?我检查过了,但找不到任何相关的东西,因为我的笔划方法在最后。这是我正在尝试的:

代码语言:javascript
运行
复制
  - (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();
}

此外,笔触颜色始终为黑色。我把它记入日志,它会显示正确的颜色。关于无当前点错误的不合理之处在于,我在点击时使用相同的参数来创建圆,它工作得很好,唯一不起作用的是直线。

EN

回答 1

Stack Overflow用户

发布于 2013-11-26 08:51:07

在这里,您需要第三点,但它应该是第二点。

代码语言:javascript
运行
复制
if(!CGPointEqualToPoint(third, CGPointZero)){
CGContextAddLineToPoint(context, second.x, second.y);
}

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

在这里,您再次调用第三点,但在正确的位置

代码语言:javascript
运行
复制
if(!CGPointEqualToPoint(third, CGPointZero)){
CGContextAddLineToPoint(context, third.x, third.y);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20206493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档