一.简介
NSImageView
和iOS的UIImageView
类似,只有添加手势时有些不一样。macOS中NSImageView
没有userInteractionEnabled
,不能添加gesture。
二.示例
要想给NSImageView
添加手势有2种方式
-
需要创建一个子类集成
NSImageView
,重写mouseDown、mouseUp等方法。// FSImageView.h #import <Cocoa/Cocoa.h> @interface FSImageView : NSImageView @property (copy, nonatomic) void(^mouseDownBlock)(void); @property (copy, nonatomic) void(^mouseUpBlock)(void); @end // FSImageView.m #import "FSImageView.h" @implementation FSImageView - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Drawing code here. } - (void)mouseDown:(NSEvent *)event { if (self.mouseDownBlock) { self.mouseDownBlock(); } } - (void)mouseUp:(NSEvent *)event { if (self.mouseUpBlock) { self.mouseUpB