osx 畫板制作

基礎(chǔ):

工具:MAC ,Xcode

運(yùn)用知識(shí):MAC手勢NSGestureRecognizer,自定義NSView,NSDrawer使用。

原理:

1.用手勢類監(jiān)控鼠標(biāo)點(diǎn)擊的坐標(biāo),存在數(shù)組中。鼠標(biāo)點(diǎn)下為一條線的start點(diǎn),結(jié)束為線的end點(diǎn),把所有的點(diǎn)都穿進(jìn)自定義View的數(shù)組中。

代碼如下:

//視圖點(diǎn)添加長按手勢。

NSPressGestureRecognizer *ges = [[NSPressGestureRecognizer alloc]initWithTarget:self action:@selector(press:)];

? ? ges.minimumPressDuration = 0.01;

? ? [self.window.contentView addGestureRecognizer:ges];

//手勢調(diào)用方法

//gesture

- (void)press:(NSGestureRecognizer *)ges{

? ? NSPoint pt = [ges locationInView:self.window.contentView];

? ? MutiPath *view = self.window.contentView;

? ? //屬性

? ? //背景顏色

? ? NSColor *bKColor = self.bkclr;

? ? //線寬

? ? float lineWidth = self.lineWidth ;

? ? //線色

? ? NSColor *lineClr = self.lineclr;

? ? if(ges.state == NSGestureRecognizerStateBegan)//鼠標(biāo)點(diǎn)下

? ? {

? ? ? ?NSLog(@"start");

? ? ? ? [view drawMutiLinesInBackgroudColor:bKColor Point:pt withLineColor:lineClr LineWidth:lineWidth end:NO];

? ? }else if(ges.state == NSGestureRecognizerStateChanged)//鼠標(biāo)移動(dòng)

? ? {

? ? ? ? NSLog(@"changed");

? ? ? ? [view drawMutiLinesInBackgroudColor:bKColor Point:pt withLineColor:lineClr LineWidth:lineWidth end:NO];

? ? }else if(ges.state == NSGestureRecognizerStateEnded)//鼠標(biāo)松開

? ? {

? ? ? ? NSLog(@"ended");

? ? ? ? [view drawMutiLinesInBackgroudColor:bKColor Point:pt withLineColor:lineClr LineWidth:lineWidth end:YES];

? ? }

}

2.自定義View處理手段.所有的點(diǎn)保存進(jìn)self.linesManage ?里面存的對象為線模型,下面為線模型屬性

#import@interface LineModel : NSObject

@property (nonatomic) NSColor *color;//線色

@property (nonatomic) NSArray *points;//線組成點(diǎn)

@property (nonatomic) BOOL isEnd;//線是否完結(jié)

@property (nonatomic) float lineWidth;//線寬

- (instancetype)init;

- (void)addpoint:(NSPoint) pt;

@end

自定義View中畫線使用 NSBezierPath。

- (void)drawRect:(NSRect)dirtyRect {

? ? [super drawRect:dirtyRect];

? ? NSBezierPath *path = [NSBezierPath bezierPathWithRect:self.bounds];

? ? if(!self.backgroud)

? ? {

? ? ? ? [[NSColor clearColor] set];

? ? }else{

? ? ? ? [self.backgroud set];

? ? }

? ? [path fill];

? ? NSLog(@"drawRect");

? ? if(self.linesManager.count !=0){

? ? [self.linesManager enumerateObjectsUsingBlock:^(LineModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {

? ? ? ? NSArray *pts = obj.points;

? ? ? ? if(pts.count > 1)

? ? ? ? {

? ? ? ? ? ? NSBezierPath *path = [NSBezierPath bezierPath];

? ? ? ? ? ? path.lineWidth = obj.lineWidth;

? ? ? ? ? ? [obj.color set];

? ? ? ? ? ? [pts enumerateObjectsUsingBlock:^(NSString *ptstr, NSUInteger idx, BOOL * _Nonnull stop) {

? ? ? ? ? ? ? ? NSPoint pt = NSPointFromString(ptstr);

? ? ? ? ? ? ? ? if(idx ==0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? [path moveToPoint:pt];

? ? ? ? ? ? ? ? }else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? [path lineToPoint:pt];

? ? ? ? ? ? ? ? ? ? [path stroke];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }];

? ? ? ? }

? ? }];}

}

3.工具抽屜類

//抽屜類初始化

- (void)drawerInit{

? if(!self.dw)

? {

? ? ? self.dw = [[NSDrawer alloc]initWithContentSize:NSMakeSize(20, 40) preferredEdge:NSRectEdgeMinY];

? ? ? self.dw.delegate = self;

? ? ? ToolBar *tbar =? [[ToolBar alloc]init];

? ? ? tbar.delegate = self;

? ? ? self.dw.contentView = tbar;

? ? ? self.dw.parentWindow = self.window;

? ? ? self.dw.leadingOffset = 0.f;

? ? ? self.dw.trailingOffset = 0.f;

? ? ? self.dw.minContentSize = NSMakeSize(100, 100);

? ? ? self.dw.maxContentSize = NSMakeSize(300, 300);

? ? ? [self.dw openOnEdge:NSRectEdgeMinY];

? }

}

4.直接附圖。

由于公司GitHub,CSND等網(wǎng)站均不能登錄,實(shí)在抱歉源碼發(fā)不出去,對mac感興趣的同學(xué)可以留言,很樂意將源碼提供大家一起學(xué)習(xí)進(jìn)步。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容