Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw line in Cocoa?

How i can draw a line in a specific window after a button click?

i'm using this:

NSBezierPath * path = [NSBezierPath bezierPath];
        [path setLineWidth: 4];

        NSPoint startPoint = {  21, 21 };
        NSPoint endPoint   = { 128,128 };
        [path  moveToPoint: startPoint];    
        [path lineToPoint:endPoint];

        [[NSColor redColor] set]; 
        [path stroke];

but it work only if i put it in the:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

how i can solve this? my goal is to create an application that can draw lines , according to the details(coordinate) received

thank you.

like image 734
Corninos Avatar asked Dec 11 '25 12:12

Corninos


1 Answers

You should not be drawing outside of a view or layer's drawing method (e.g. drawRect:). What you want to do, in broad strokes, is have a view there that draws the line when a flag is set, and when you click the button, set the flag and tell the view to redraw.

like image 94
Chuck Avatar answered Dec 14 '25 05:12

Chuck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!