【问题标题】:NSImageView double click actionNSImageView 双击动作
【发布时间】:2012-12-07 08:02:03
【问题描述】:

我的 Mac 应用程序中有一些 NSImageView,用户可以在其中拖放 .png 或 .pdf 等对象,将它们存储到用户共享默认值中,效果很好。

我现在想为用户双击这些 NSImageView 设置一个操作,但这似乎有点困难(我对 NSTableView 没有任何问题,但 NSImage 无法使用“setDoubleAction”,而且还有很多关于 NSImageView 操作的答案(此处或与 google 一起)指向制作 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];

}

但这不起作用。

谁能告诉我最简单的方法是什么?

【问题讨论】:

    标签: xcode nsimageview


    【解决方案1】:

    您需要继承 NSImageView 并将以下方法添加到子类的实现中:

    - (void)mouseDown:(NSEvent *)theEvent
    {
        NSInteger clickCount = [theEvent clickCount];
    
        if (clickCount > 1) {
            // User at least double clicked in image view
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        使用extension的另一种解决方案:

        extension NSImageView {
            override open func mouseDown(with event: NSEvent) {
                // your code here
            }
        }
        

        虽然这会为每个 NSImageView 添加该功能,但也许这不是您想要的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-03
          • 2011-08-02
          • 2019-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多