Reputation: 81
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.
Upvotes: 0
Views: 450
Reputation: 1562
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++;
}
}
Upvotes: 4
Reputation: 354
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.
Upvotes: 0
Reputation: 14694
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.
Upvotes: 1