I have an NSEvent and want to detect when a rect is clicked so this is the code I have:
- (void)mouseDown:(NSEvent *)event;
{
NSPoint clickedPoint = [event locationInWindow];
//perform code for clickedPoint
}
Instead of using locationInWindow how can I convert a rect or view to a NSPoint so that it will check if I clicked the rect? thanks!
First off, consider whether you really want to override
mouseDown:or whether overridingmouseUp:might be a better option. If this click is similar to something like clicking a button, it’s generally a better idea to overridemouseUp:rather thanmouseDown:, asmouseUp:would allow the user to “change their mind” by dragging the mouse out of the button’s rect before letting go of the mouse.NSEvent’slocationInWindowgives the location of the event in the base window’s coordinates. To convert that location to the view’s local coordinate system, you can useNSView‘s convertPoint:fromView: like in the following:See Cocoa Event-Handling Guide: Getting the Location of an Event for more information.