一、基本使用
//创建ImageVier
NSImageView *imgView = [[NSImageView alloc]init];
//设置大小
imgView.frame = NSMakeRect(0, 0, 300, 300);
//设置样式 图片边框的样式
//NSImageFrameNone 无边框
//NSImageFramePhoto, //相框(黑色的边线,以及阴影背景)
//NSImageFrameGrayBezel, //灰色
//NSImageFrameGroove, //雕刻木槽边框
//NSImageFrameButton //仿按键框
//imgView.imageFrameStyle = NSImageFramePhoto;
//设置背景颜色
//imgView.wantsLayer = YES;
//imgView.layer.backgroundColor = [NSColor cyanColor].CGColor;
//设置显示的图片
imgView.image = [NSImage imageNamed:@"qqqq"];
//设置缩放方式
imgView.imageScaling = NSImageScaleNone;
//是否可以编辑 用户能否直接将图片拖到一个NSImageView类里
[imgView setEditable:FALSE];
//设置动画 默认是关闭的,使用storyboard创建是为NO(动画文件要不是拖到Assets.xcassets中)
imgView.animates = YES;
//图片内容对于控件的位置
imgView.imageAlignment = NSImageAlignTopRight;
//表示用户能否对图片内容进行剪切、复制、粘贴行操作
[imgView setAllowsCutCopyPaste:YES];
//添加圆角
imgView.wantsLayer = YES;
imgView.layer.cornerRadius = 100;
[self.view addSubview:imgView];
二、处理图片事件响应。
方式一
//苹果不人推荐使有nsimageview来响应用户操作,建议使用nsbutton.
//仅当图片可以编辑时,用户拖拽图片到imageview时,才会执行响应。
[imgView setTarget:self];
[imgView setAction:@selector(click)];
方式二(推荐方法)
//在imageview上一个跟图片大小相同的NSButton来响就事件
//这个NSButton是透明没有边框,没有Tittle.
方式三
//创建图片识别器
NSClickGestureRecognizer *clickGes = [[NSClickGestureRecognizer alloc]init];
clickGes.target = self;
clickGes.action = @selector(clickaaa);
[imgView addGestureRecognizer:clickGes];
方式四
//自定义类,重写- (void)mouseDown:(NSEvent *)event方法
//下面方法用来调用action
[NSApp sendAction:self.action to:self.target from:self]