Question

I am trying to draw a simple diagram. I have squares that are connected by lines. The lines are drawn with NSBezierPath's. I am using a random color for the lines so I can follow them. My problem is that the lines change color.

The output -

enter image description here

Code - I removed the lines that draw the squares so it only draws the lines:

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);

    CGFloat height = 70.0f;
    CGFloat yOffset = 20.0f;

    NSRect rect = NSMakeRect(50.0f, 50.0f, 200.0f, height);

    NSEnumerator *reverseEnumerator = [steps reverseObjectEnumerator];

    NSMutableDictionary *rectsByStep = [NSMutableDictionary dictionaryWithCapacity:10];

    for( WFGJobStep *step in reverseEnumerator )
    {
        [[NSColor redColor] setFill];


        [rectsByStep setObject:[NSValue valueWithRect:rect] forKey:[NSNumber numberWithInteger:step.step]];

        rect.origin.y += height + yOffset;

    }

    reverseEnumerator = [steps reverseObjectEnumerator];
    for( WFGJobStep *step in reverseEnumerator )
    {
        NSRect stepRect = [[rectsByStep objectForKey:[NSNumber numberWithInteger:step.step]] rectValue];
        NSPoint startPoint = NSMakePoint( stepRect.origin.x + stepRect.size.width, stepRect.origin.y);

        //  draw lines
        for( NSNumber *stepNumber in step.nextSteps )
        {

        //
        //
        //  Line drawing code here
        //
        //

            NSBezierPath * path = [NSBezierPath bezierPath];
            [path setLineWidth: 4];

            NSRect targetStepRect = [[rectsByStep objectForKey:stepNumber] rectValue];
            NSPoint endPoint = NSMakePoint( targetStepRect.origin.x + targetStepRect.size.width, targetStepRect.origin.y + targetStepRect.size.height);

            [path moveToPoint:startPoint];

            CGFloat controlX = ( startPoint.y - endPoint.y ) * .2 + stepRect.origin.x + stepRect.size.width + 20;

            [path curveToPoint:endPoint controlPoint1:NSMakePoint(controlX, startPoint.y) controlPoint2:NSMakePoint(controlX, endPoint.y)];

            NSRect square = NSMakeRect( endPoint.x, endPoint.y, 9, 9 );
            [path appendBezierPathWithOvalInRect: square];

            [[self randomColor] set];
            [path stroke];
        }
    }
}

- (NSColor *)randomColor {
    float c[4];

    c[0] = (arc4random() / RAND_MAX) * 1;
    c[1] = (arc4random() / RAND_MAX) * 1;
    c[2] = (arc4random() / RAND_MAX) * 1;
    c[3] = 1.0f;

    return [NSColor colorWithDeviceRed:c[0] green:c[1] blue:c[2] alpha:c[3]];
}
Was it helpful?

Solution

My issue is that this is in a scoll view and this is my first NSScrollView. I was not aware that as the view scrolls it redraws its subviews. The issue was that I was using a random color for my stroke so that as I scrolled it would randomly change the colors of the lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top