NSImageView double click action

让人想犯罪 __ 提交于 2019-12-05 01:48:58

You need to subclass NSImageView and add the following method to your subclass's implementation:

- (void)mouseDown:(NSEvent *)theEvent
{
    NSInteger clickCount = [theEvent clickCount];

    if (clickCount > 1) {
        // User at least double clicked in image view
    }
}

Code for Swift 4. Again the NSImageView is subclassed and the mouseDown function is overridden.

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

}

Another solution using extension:

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

Although this will add that functionality to every NSImageView, so perhaps that's not what you're looking for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!