真的很奇怪drawRect的行为?

时间:2013-12-25 05:47:50

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

我有一个非常简单的应用程序,用户可以绘制。用户点击3次,并在这3个点击处绘制一个角度。在每个水龙头的位置,有一个圆圈。下面是它的样子:

在第一次点按时,只创建一个圆圈,因为没有其他要连接的点(应该如此)。在第二个水龙头上,它连接第一个到第二个水龙头的一条线(应该如此)。在第三次敲击时,它变得奇怪并从第二个圆圈的边缘(应该在中间?)连接到第三个水龙头。任何想法为什么在地球上发生这种情况?

3次点击后:

enter image description here

3个水龙头的另一个例子:

enter image description here

这就是我的绘画方式:

- (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 = [touch locationInView:self];
        second = CGPointZero;
        third = CGPointZero;
    }

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

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

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

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

        CGContextStrokePath(context);
    }
}

1 个答案:

答案 0 :(得分:2)

因为在绘制第二个椭圆CGContextAddEllipseInRect(context, rectangle2);后您的当前位置发生了变化 试试这个:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor * w = [UIColor whiteColor] ;
    UIColor * r = [UIColor redColor] ;
    CGContextSetStrokeColorWithColor(context, w.CGColor) ;
    CGContextSetFillColorWithColor(context, r.CGColor) ;

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

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

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

        CGContextStrokePath(context);
    }
}