Can NSView and NSImageView respond to Mouse Clicks?


Dave
 

Hi,

Can an Custom NSView respond to Mouse Clicks via IBActions?

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.

All the Best
Dave


Keary Suska
 

NSView doesn’t have action handling—you get that from NSControl. If you need action handling, you might need an NSControl subclass rather than an NSView subclass. That being said, you can call your action method “manually” in an event handler for NSView.

As for NSImageView, It doesn’t look like you can specify action handling in Xcode. Have you tried calling -sendActionOn: on your image view?

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"

On Sep 19, 2018, at 12:34 PM, Dave <dave@looktowindward.com> wrote:

Hi,

Can an Custom NSView respond to Mouse Clicks via IBActions?

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.


Andy Lee
 

On Sep 19, 2018, at 2:34 PM, Dave <dave@looktowindward.com> wrote:
Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..
From the docs:

<https://developer.apple.com/documentation/appkit/nsimageview?language=objc>
Note
An image view calls its action method only when the user drags an image into the image view's bounds, and the image view must be editable to receive dragged images. If you want to display an image and respond to clicks in the image, use an NSButton object instead.
--Andy


Alex Zavatone
 

You can add a gesture to it IIRC.

On Sep 19, 2018, at 1:34 PM, Dave <dave@looktowindward.com> wrote:

Hi,

Can an Custom NSView respond to Mouse Clicks via IBActions?

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.

All the Best
Dave


Dave
 

Hi,

This is for NSView, I’ve not looked into NSImageView as yet.

I tried adding a Gesture Recognizer which sort of work except the Handler gets calls more than nice when I mouse down.

Basically, I want to detect a Mouse Down on an NSView Subclass and when it occurs display a Pop-Up Menu. Don’t understand why I'm getting multiple calls - see code below.

All the Best
Dave

-(instancetype) initWithCoder:(NSCoder*) theDecoder
{
self = [super initWithCoder:theDecoder];
if (self == nil)
return nil;

NSLog(@"initWithCoder: %@",self.identifier);
[self setupView];

return self;
}


-(void) setupView
{
NSPressGestureRecognizer* myGestureRecognizer;

myGestureRecognizer = [[NSPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionPressGesture:)];
myGestureRecognizer.minimumPressDuration = 0.001;
myGestureRecognizer.buttonMask = 1;
myGestureRecognizer.delaysPrimaryMouseButtonEvents = NO;
[self addGestureRecognizer:myGestureRecognizer];
}

-(void) actionPressGesture:(LTWTestViewCell*) theSender
{
NSLog(@"actionPressGesture: %@",self.identifier);
}

On 20 Sep 2018, at 02:03, Alex Zavatone via Groups.Io <zav=mac.com@groups.io> wrote:

You can add a gesture to it IIRC.
On Sep 19, 2018, at 1:34 PM, Dave <dave@looktowindward.com> wrote:

Hi,

Can an Custom NSView respond to Mouse Clicks via IBActions?

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.

All the Best
Dave



Sean McBride
 

On Thu, 20 Sep 2018 11:36:10 +0200, Dave said:

Basically, I want to detect a Mouse Down on an NSView Subclass and when
it occurs display a Pop-Up Menu.
Suggestion: open NSView.h and search for "menu", there are API like menuForEvent: and willOpenMenu: that look useful for your case.

Cheers,

Sean


Jack Brindle
 

NSView inherits from NSResponder. That gives NSView access to mouseDown, mouseUp and a whole host of other important calls.
What this really means is that you can implement
- (void)mouseDown:(NSEvent *)event;
or
v- (void) mouseUp:(NSEvent *)event;
in your NSView subclass and handle mouse events the way you like, which includes generating IBAction calls to other objects that need to be notified of mouse events in your view.
The event object will contains information about where the cursor was when the mouse down or up even occurred, which you can use to determine exactly where in the view the hit occurred, and even when.
Note that any control embedded in the view will take precedence over the view itself, so that it gets a chance to handle the mouse down first.

After all these years, I still find it really cool the way events and the responder chain works, and gives us so much power to create great user interfaces.

Jack

On Sep 20, 2018, at 4:36 AM, Dave <dave@looktowindward.com> wrote:

Hi,

This is for NSView, I’ve not looked into NSImageView as yet.

I tried adding a Gesture Recognizer which sort of work except the Handler gets calls more than nice when I mouse down.

Basically, I want to detect a Mouse Down on an NSView Subclass and when it occurs display a Pop-Up Menu. Don’t understand why I'm getting multiple calls - see code below.

All the Best
Dave

-(instancetype) initWithCoder:(NSCoder*) theDecoder
{
self = [super initWithCoder:theDecoder];
if (self == nil)
return nil;

NSLog(@"initWithCoder: %@",self.identifier);
[self setupView];

return self;
}


-(void) setupView
{
NSPressGestureRecognizer* myGestureRecognizer;

myGestureRecognizer = [[NSPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionPressGesture:)];
myGestureRecognizer.minimumPressDuration = 0.001;
myGestureRecognizer.buttonMask = 1;
myGestureRecognizer.delaysPrimaryMouseButtonEvents = NO;
[self addGestureRecognizer:myGestureRecognizer];
}

-(void) actionPressGesture:(LTWTestViewCell*) theSender
{
NSLog(@"actionPressGesture: %@",self.identifier);
}


On 20 Sep 2018, at 02:03, Alex Zavatone via Groups.Io <zav=mac.com@groups.io> wrote:

You can add a gesture to it IIRC.
On Sep 19, 2018, at 1:34 PM, Dave <dave@looktowindward.com> wrote:

Hi,

Can an Custom NSView respond to Mouse Clicks via IBActions?

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.

All the Best
Dave





Dave
 

Thanks a lot, I actually found this yesterday and now have it working using the mouseDown method to display a Popup - work a treat!

All the Best
Dave

On 20 Sep 2018, at 23:32, Jack Brindle via Groups.Io <jackbrindle@...> wrote:

NSView inherits from NSResponder. That gives NSView access to mouseDown, mouseUp and a whole host of other important calls. 
What this really means is that you can implement
- (void)mouseDown:(NSEvent *)event;
or
v- (void) mouseUp:(NSEvent *)event;
in your NSView subclass and handle mouse events the way you like, which includes generating IBAction calls to other objects that need to be notified of mouse events in your view.
The event object will contains information about where the cursor was when the mouse down or up even occurred, which you can use to determine exactly where in the view the hit occurred, and even when.
Note that any control embedded in the view will take precedence over the view itself, so that it gets a chance to handle the mouse down first. 

After all these years, I still find it really cool the way events and the responder chain works, and gives us so much power to create great user interfaces.

Jack



On Sep 20, 2018, at 4:36 AM, Dave <dave@...> wrote:

Hi,

This is for NSView, I’ve not looked into NSImageView as yet.

I tried adding a Gesture Recognizer which sort of work except the Handler gets calls more than nice when I mouse down.

Basically, I want to detect a Mouse Down on an NSView Subclass and when it occurs display a Pop-Up Menu. Don’t understand why I'm getting multiple calls - see code below.

All the Best
Dave

-(instancetype) initWithCoder:(NSCoder*) theDecoder
{
self = [super initWithCoder:theDecoder];
if (self == nil)
return nil;

NSLog(@"initWithCoder: %@",self.identifier);
[self setupView];

return self;
}


-(void) setupView
{
NSPressGestureRecognizer* myGestureRecognizer;

myGestureRecognizer = [[NSPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionPressGesture:)];
myGestureRecognizer.minimumPressDuration = 0.001;
myGestureRecognizer.buttonMask = 1;
myGestureRecognizer.delaysPrimaryMouseButtonEvents = NO;
[self addGestureRecognizer:myGestureRecognizer];
}

-(void) actionPressGesture:(LTWTestViewCell*) theSender
{
NSLog(@"actionPressGesture: %@",self.identifier);
}


On 20 Sep 2018, at 02:03, Alex Zavatone via Groups.Io <zav@...> wrote:

You can add a gesture to it IIRC.
On Sep 19, 2018, at 1:34 PM, Dave <dave@...> wrote:

Hi,

Can an Custom NSView respond to Mouse Clicks via IBActions? 

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.

All the Best
Dave