NSBezierPath 独特的线条

发布于 2024-12-15 07:57:06 字数 986 浏览 6 评论 0原文

我正在制作一个简单的绘图应用程序,并使用 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 路径。

I am making a simple drawing app and am using NSBezierPath to draw the lines. I am subclassing NSView. I need to make a method which allows the user to change the color and size of the next path (so the user presses a button, then the next time they draw a path it is the specified color/size) but right now when I try doing that it changes the color and size of all EXISTING paths. How can I make them "individual", so to speak? Here is my code:

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

So I need to make a individual NSBezierPath paths.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送舟行 2024-12-22 07:57:06

您必须使用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++;
}

    }

You have to use 2 mutable arrays(bezierpaths &color) , one integer variable(brush size).
and one UIColor variable for brushColor

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

    }
深海少女心 2024-12-22 07:57:06

听起来您想在 mouseDown 上开始一个新路径,否则您所做的就是将行附加到现有路径。

我的建议是使用 NSMutableArray 来保存您的路径,然后您可以使用 [myArray objectAtIndex:myIndex] 找到特定路径来更改颜色。

It sounds like you want to start a new path on mouseDown, otherwise all you are doing is appending lines to the existing path.

My suggestion is to have a NSMutableArray to hold your paths and then you can find a specific path with [myArray objectAtIndex:myIndex] to change the color.

丑疤怪 2024-12-22 07:57:06

我觉得我们缺少一些代码来真正理解这一点,但据我所知,你只有一条路。实际上,我对这个片段感到惊讶的是,路径的颜色会发生变化,因为每次绘制时,您都使用灰色来绘制并且宽度相同。

此外,在 mouseDown 中,您总是向最后一个路径添加一行。整条路径只能有一种颜色。您每次都需要创建一个新路径,并通过子类化或具有混合结构来保存其颜色。主要思想是,一条 BezierPath 只能有一种颜色和一种描边宽度。

I feel we are missing some code to really understand this, but from what I can understand, you only have one path. I am actually surprised from this snippet that the color of your path changes since every time you draw, you are using the gray color to draw and the same width.

Furthermore, in mouseDown, you are always add a line to the last path. The entire path can only have one color. You would need to create a new path every time and save its color either by subclassing or having a hybrid structure. Main idea, one BezierPath can only have one color and one stroke width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文