gpt4 book ai didi

objective-c - NSBezierPath 独特的线条

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

我正在制作一个简单的绘图应用程序,并使用 NSBezierPath 来绘制线条。我正在对 NSView 进行子类化。我需要制定一种方法,允许用户更改下一条路径的颜色和大小(因此用户按下一个按钮,然后下次他们绘制路径时,它就是指定的颜色/大小),但现在当我尝试时这样做会改变所有现有路径的颜色和大小。可以这么说,我怎样才能使它们成为“个体”呢?这是我的代码:

- (void)drawRect:(NSRect)dirtyRect
{


[path setLineWidth:5];

[path setLineJoinStyle:NSRoundLineJoinStyle];
[path setLineCapStyle:NSRoundLineCapStyle];

[path stroke];


}

- (void)mouseDown:(NSEvent *)theEvent {

NSPoint location = [theEvent locationInWindow];
NSLog(@"%f, %f", location.x, location.y);

[path moveToPoint:location];
[self setNeedsDisplay:YES];

}

- (void)mouseUp:(NSEvent *)theEvent {

}

- (void)mouseDragged:(NSEvent *)theEvent {

NSPoint location = [theEvent locationInWindow];
[path lineToPoint:location];
[self setNeedsDisplay:YES];

}

- (void)changeBrushColor:(NSString *)color {

// change color of the next path

[self setNeedsDisplay:YES]; // show it
}

所以我需要创建一个单独的 NSBezierPath 路径。

最佳答案

你必须使用2个可变数组(贝塞尔路径和颜色),一个整数变量(画笔大小)。和一个用于 BrushColor 的 UIColor 变量

    -(IBAction) brushsizeFun
{
brushSize = 30; // any brush size here. better use a slider here to select size
}

-(IBAction) brushColorFun
{
brushColor = [UIColor redColor]; // Any color here. better use a color picker
}


- (void)mouseDown:(NSEvent *)theEvent {

NSPoint location = [theEvent locationInWindow];
NSLog(@"%f, %f", location.x, location.y);
[path release];
path = [[UIBezierpath alloc]init];
path.lineWidth = brushSize;
[path moveToPoint:location];
[bezierArray addObject:path];
[colorArray addObject:brushPattern];


[self setNeedsDisplay:YES];

}

- (void)drawRect:(NSRect)dirtyRect
{
int q=0;
//Draw the bezierpath and corresonding colors from array
for (UIBezierPath *_path in bezierArray)
{
UIColor *_color = [colorArray objectAtIndex:q];
[_color setStroke];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
q++;
}

}

关于objective-c - NSBezierPath 独特的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8108479/

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