0

I have a simple bezierPath with 2 elements in a NSView; I want to modify the last element (NSPoint) on a button pressed but my code don't have any visual effect on the path. Here my code in NSView subclass:

    NSBezierPath *path;    
    - (void)drawRect:(NSRect)dirtyRect {
        [super drawRect:dirtyRect];
        // Drawing code here.

        path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(0, 0)];
        [path lineToPoint:NSMakePoint(60, 60)];
        [path setLineWith:2.0];
        [[NSColor redColor] set];
        [path stroke]; 
        //the path is correctly drawing and visible
    }

    - (IBAction)buttonPressed:(id)sender {
        NSPoint newPoint = NSMakePoint(120, 120);
        [path setAssociatedPoints:&newPoint atIndex:1]; //has no visible effect
   }

any suggestion ?

mattiad
  • 13
  • 1
  • 6

2 Answers2

1

Every time you call drawRect: you are creating a new path and drawing it. Then, on the button press you modify the path.

So you have 2 problems:

  1. You keep recreating the path - just create it once when the view is created
  2. You don't redraw the view when the path is updated - use setNeedsDisplay
Wain
  • 118,658
  • 15
  • 128
  • 151
0

You are recreating the Bezier path each time through your -drawRect: method. So, it doesn't matter that you kept the last one and modified it. You are discarding that and creating a new one the next time your view draws.

Also, if your variable is really just declared outside of any curly braces ({}), then it's not an instance variable. It's just a file-scope global variable. That means it's shared by all instances of this view class.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154