我的Mac App中有一些NSImageView,用户可以拖放像.png或.pdf这样的对象,将它们存储到用户共享的默认设置中,这很好用。
我现在想为用户双击这些NSImageView设置一个动作,但这似乎有点困难(我在NSTableView上没有问题,但'setDoubleAction‘在NSImage上不可用,而且关于NSImageView的动作的大量答案(这里或谷歌)都指向制作NSButton而不是NSImageView,所以这没有帮助)
下面是我的AppDelegate.h的一部分:
@interface AppDelegate : NSObject <NSApplicationDelegate>{
(...)
@property (assign) IBOutlet NSImageView *iconeStatus;
(...)
@end下面是我的AppDelegate.m的一部分:
#import "AppDelegate.h"
@implementation AppDelegate
(...)
@synthesize iconeStatus = _iconeStatus;
(...)
- (void)awakeFromNib {
(...)
[_iconeStatus setTarget:self];
[_iconeStatus setAction:@selector(doubleClick:)];
(...)
}
(...)
- (void)doubleClick:(id)object {
//make sound if that works ...
[[NSSound soundNamed:@"Basso"] play];
}但这并不管用。
谁能告诉我做这件事最简单的方法是什么?
发布于 2013-07-15 22:18:14
您需要子类NSImageView,并将以下方法添加到子类的实现中:
- (void)mouseDown:(NSEvent *)theEvent
{
NSInteger clickCount = [theEvent clickCount];
if (clickCount > 1) {
// User at least double clicked in image view
}
}发布于 2018-05-02 03:29:20
Swift 4的代码。同样,NSImageView被子类化,mouseDown函数被覆盖。
class MyImageView: NSImageView {
override func mouseDown(with event: NSEvent) {
let clickCount: Int = event.clickCount
if clickCount > 1 {
// User at least double clicked in image view
}
}
}发布于 2018-06-22 00:17:39
另一种使用extension的解决方案
extension NSImageView {
override open func mouseDown(with event: NSEvent) {
// your code here
}
}虽然这会将该功能添加到每个NSImageView中,但这可能不是您想要的。
https://stackoverflow.com/questions/13961356
复制相似问题