我的 NSImageView 未检测到 mouseDown

发布于 2024-11-28 12:37:25 字数 589 浏览 3 评论 0原文

我一直在寻找这个问题的答案,虽然我已经完成了这些步骤,但我似乎仍然无法调用 mouseDown 方法。 我想要的是当用户单击并按住图像时,它应该打印出“鼠标按下!”。 (最终我想做的是用户单击并按住图像以播放声音,当他们松开时声音就会停止)。

这就是我所拥有的。我的标题:

@interface MyNSImageView : NSImageView <NSImageDelegate> {
}
@end

我的班级:

@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

这看起来正确吗?如果是这样,那么是否还有其他问题可能会干扰它?不然我该怎么办?

多谢多谢!

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 should 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2024-12-05 12:37:25

这是我的解决方案:

在 NSImageView 上方添加一个没有边框或标题的不可见按钮

:)

Here's my solution:

Add an invisible button without border or title above the NSImageView.

:)

浅忆 2024-12-05 12:37:25

第一个想法:尝试实现 acceptsFirstResponder 并返回 yes。然后让你的委托将第一响应者设置为图像视图...

更新:

所以,我刚刚创建了一个新项目,并向其中添加了一个类 MyClass,它继承自 NSImageView。 mouseDown 的实现:

-(void)mouseDown:(NSEvent *)theEvent
{
NSLog(@"Mouse Down");
}

和 initWithFrame 的实现:

- (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;
}

使视图可见。

然后,我通过向窗口添加自定义类并将自定义类的类更改为 MyClass 来编辑 nib 文件。它工作得很好,所以我不知道出了什么问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文