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 路径。

4

3 回答 3

4

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

    }
于 2012-08-01T10:40:13.877 回答
1

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

我的建议是有一个 NSMutableArray 来保存你的路径,然后你可以找到一个特定的路径[myArray objectAtIndex:myIndex]来改变颜色。

于 2011-11-13T00:36:51.167 回答
0

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

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

于 2011-11-13T00:40:06.770 回答