我正在制作一个简单的绘图应用程序,并使用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个可变数组(bezierpaths&color),一个整数变量(画笔大小)。
一个UIColor变量用于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++;
}

    }

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

10-12 04:55