gpt4 book ai didi

objective-c - NSBezierPath 上的剥离描边

转载 作者:行者123 更新时间:2023-12-03 17:41:21 25 4
gpt4 key购买 nike

我正在尝试画一个简单的图表。我有由线连接的正方形。这些线条是用 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]];
}

最佳答案

我的问题是这是在 scoll View 中,这是我的第一个 NSScrollView。我不知道当 View 滚动时它会重新绘制其 subview 。问题是我为笔画使用了随机颜色,因此当我滚动时,它会随机改变线条的颜色。

关于objective-c - NSBezierPath 上的剥离描边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13120159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com