I've been searching for answers to this question and while I have done the steps I still cannot seem to get the mouseDown method to be called. What I want is when the user clicks and holds down on the image, it sh开发者_开发问答ould print out "Mouse down!". (Eventually what I want to do is the user to click and hold on the image to cause a sound to play, and when they let go the sound will stop).
Here's what I have. My header:
@interface MyNSImageView : NSImageView <NSImageDelegate> {
}
@end
And my class:
@implementation MyNSImageView
- (void)mouseDown:(NSEvent *)event {
if ([[self target] respondsToSelector:[self action]]) {
[NSApp sendAction:[self action] to:[self target] from:self];
}
NSLog(@"Mouse Down!");
}
@end
Does this look right? If so, then are there other problems which might be interfering with it? Of not, what should I do?
Thanks heaps!
Here's my solution:
Add an invisible button without border or title above the NSImageView.
:)
First thought: Try implementing acceptsFirstResponder
and return yes. Then have your delegate set the first responder to the image view....
UPDATE:
So, I just created a new project, and added a class MyClass to it, which inherits from NSImageView. mouseDown is implemented:
-(void)mouseDown:(NSEvent *)theEvent
{
NSLog(@"Mouse Down");
}
and initWithFrame is implemented:
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
NSURL *newURL = [[NSBundle mainBundle] URLForImageResource:@"ARandomImageIAddedtoProject"];
NSImage *newImage = [[NSImage alloc] initWithContentsOfURL:newURL];
[self setImage:newImage];
}
return self;
}
To make the view visible.
I then edited the nib file by adding a custom class to the window, and changing the class of the custom class to MyClass. It worked fine, so I dunno whats wrong.
精彩评论