Separate names with a comma.
Discussion in 'Mac Programming' started by Blakeasd, Feb 14, 2011.
I am trying to draw into a view using the mouseDragged event. How can I go about doing this?
Grab the mouse location relative to your view (e.g. [self convertPoint:[event locationInWindow] fromView:nil]) and store it, and redraw yourself (setNeedsDisplay. Then in your drawRect: retrieve that point and draw it.
I understand how to get the mouse location. How do I draw it though?
Look at the documentation for NSBezierCurve, you might find something you can use in its methods.
Do I use the stroke method?
Are you expecting to end up with a path-based image (drawing) or bitmap image (painting)?
If you want to "paint", you will be repeatedly applying a colored shape to the cursor location. Typically, you would use -mouseDown: to begin the application of the paint image and -mouseDragged: for repeated application (tracking the mouse). You would probably lock focus on a NSImage object, apply the "paint" to it, then tell the view to draw the part of the NSImage you just changed (this means the coördinate systems of the view and the image object have to line up properly for the drawing to show up right under the mouse.
The brush will typically be a small shape, like an oval, rectangle or path outline defined in NSBezierPath. -stroke will outline the shape while -fill will paint it in. Use the method that seems appropriate for your needs (you could write the code, test it with one method, then test it with the other).
Or you could use a NSImage to serve as your brush, drawing it into your target image the same way.
For my purposes I just need to draw very simple shapes. So I think a path-based image would be best, how do I achieve that?
Figure out how to create a NSBezierPath object in the shape you want, how to center it on the mouse location, how to apply it as needed to the target image and how to keep your view updated. Documentation is your friend.