基礎(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)步。