NSBeizerPath苹果官方文档详见:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBezierPath_Class/Reference/Reference.html
一:绘制带箭头的矩形,如图所示
NSRect theRect = NSInsetRect(self.bounds, kBeizerPathLineWidth * 2, kArrowHeight);
NSBezierPath *bezierPath = [NSBezierPath bezierPath];
NSPoint arrowLeft = NSMakePoint(NSMidX(theRect) - kArrowWidth/2, NSMaxY(theRect));
NSPoint arrowRight = NSMakePoint(NSMidX(theRect) + kArrowWidth/2, NSMaxY(theRect));
NSPoint arrowTop = NSMakePoint(NSMidX(theRect), NSMaxY(self.bounds));
//画成直角的矩形
[bezierPath moveToPoint:NSMakePoint(kBeizerPathLineWidth, kArrowHeight)];
[bezierPath lineToPoint:NSMakePoint(NSMinX(theRect), NSMaxY(theRect))];
[bezierPath lineToPoint:arrowLeft];
[bezierPath lineToPoint:arrowTop];
[bezierPath lineToPoint:arrowRight];
[bezierPath lineToPoint:NSMakePoint(NSMaxX(theRect), NSMaxY(theRect))];
[bezierPath lineToPoint:NSMakePoint(NSMaxX(theRect), kArrowHeight)];
[bezierPath lineToPoint:NSMakePoint(kBeizerPathLineWidth, kArrowHeight)];
[bezierPath setLineWidth:kBeizerPathLineWidth];
[[NSColor blackColor] setStroke];
[bezierPath stroke];
二:带箭头的圆角矩形,如图所示
NSRect theRect = NSInsetRect(self.bounds, kBeizerPathLineWidth * 2, kArrowHeight);
NSBezierPath *bezierPath = [NSBezierPath bezierPath];
NSPoint arrowLeft = NSMakePoint(NSMidX(theRect) - kArrowWidth/2, NSMaxY(theRect));
NSPoint arrowRight = NSMakePoint(NSMidX(theRect) + kArrowWidth/2, NSMaxY(theRect));
NSPoint arrowTop = NSMakePoint(NSMidX(theRect), NSMaxY(self.bounds));
//画成圆角的矩形
NSPoint topLeft = NSMakePoint(NSMinX(theRect), NSMaxY(theRect));
NSPoint topRight = NSMakePoint(NSMaxX(theRect), NSMaxY(theRect));
NSPoint bottomLeft = NSMakePoint(NSMinX(theRect), NSMinY(theRect));
NSPoint bottomRight = NSMakePoint(NSMaxX(theRect), NSMinY(theRect));
[bezierPath moveToPoint:NSMakePoint(NSMinX(theRect), kRadius + kArrowHeight)];
[bezierPath lineToPoint:NSMakePoint(NSMinX(theRect), NSMaxY(theRect) - kRadius)];
[bezierPath curveToPoint:NSMakePoint(kRadius, NSMaxY(theRect)) controlPoint1:topLeft controlPoint2:topLeft];
[bezierPath lineToPoint:arrowLeft];
[bezierPath lineToPoint:arrowTop];
[bezierPath lineToPoint:arrowRight];
[bezierPath lineToPoint:NSMakePoint(NSMaxX(theRect) - kRadius, NSMaxY(theRect))];
[bezierPath curveToPoint:NSMakePoint(NSMaxX(theRect), NSMaxY(theRect) - kRadius) controlPoint1:topRight controlPoint2:topRight];
[bezierPath lineToPoint:NSMakePoint(NSMaxX(theRect), kRadius + kArrowHeight)];
[bezierPath curveToPoint:NSMakePoint(NSMaxX(theRect) - kRadius, NSMinY(theRect)) controlPoint1:bottomRight controlPoint2:bottomRight];
[bezierPath lineToPoint:NSMakePoint(kRadius, NSMinY(theRect))];
[bezierPath curveToPoint:NSMakePoint(NSMinX(theRect), kRadius + kArrowHeight) controlPoint1:bottomLeft controlPoint2:bottomLeft];
[bezierPath setLineWidth:kBeizerPathLineWidth];
[[NSColor blackColor] setStroke];
[bezierPath stroke];
方法很简单,就是在View上算坐标,然后使用- (void)moveToPoint:(NSPoint)aPoint,连线到指定的点
对于圆角矩形,使用- (void)curveToPoint:(NSPoint)aPoint controlPoint1:(NSPoint)controlPoint1 controlPoint2:(NSPoint)controlPoint2连接到指定的点。
对2个控制点的参数说明:
设置一下Window的效果,那么就是PopOver的效果了,源码见:https://github.com/surrenderios