MAC:NSImageView

2016-08-19  本文已影响145人  L柠_檬
目录
  1.1 设置圆角
  1.2 添加点击事件
1.1 设置圆角

NSImageView *imgView = [[NSImageView 
alloc]initWithFrame:CGRectMake(35, 100, 70, 70)];

//给图片赋值和iOS开发是一样的

imgView.image = [NSImage imageNamed:@"1"];

[self.view addSubview:imgView];


//设置圆角,wantLayer = YES 必须写在最前面。
imgView.wantsLayer = YES;

imgView.layer.cornerRadius = 35.0f;

imgView.layer.borderWidth = 2;

imgView.layer.borderColor = [NSColor greenColor].CGColor;

imgView.layer.masksToBounds = YES;

1.2 添加点击事件

//重写NSImageView方法
#import <Cocoa/Cocoa.h>

@interface DDImageView : NSImageView

typedef void(^BlockImageViewClicked) ();

@property (nonatomic , copy)BlockImageViewClicked blockImageViewClicked;

@end


#import "DDImageView.h"

@implementation DDImageView

- (id)initWithFrame:(NSRect)frameRect{
    
    if (self == [super initWithFrame:frameRect]) {
        
    }
    return self;
}
//重写
- (void)mouseDown:(NSEvent *)theEvent {
    
    if (self.blockImageViewClicked) {
        self.blockImageViewClicked();
    }
    
}

@end

上一篇 下一篇

猜你喜欢

热点阅读