NSBezierPath上的剥离笔划

时间:2012-10-29 10:49:31

标签: objective-c cocoa core-graphics nsbezierpath

我试图绘制一个简单的图表。我有通过线连接的正方形。这些线条是用NSBezierPath绘制的。我使用随机颜色的线条,所以我可以遵循它们。我的问题是线条会改变颜色。

输出 -

enter image description here

代码 - 我删除了绘制正方形的线条,因此它只绘制线条:

- (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]];
}

1 个答案:

答案 0 :(得分:2)

我的问题是这是一个scoll视图,这是我的第一个NSScrollView。我不知道,当视图滚动时,它会重绘其子视图。问题是我使用了一个随机颜色作为我的笔触,所以当我滚动它会随机改变线条的颜色。