NSBezierPath绘制PopOver

本文介绍如何使用 NSBezierPath 在 macOS 应用中绘制带箭头的矩形和圆角矩形。通过计算坐标点并运用 moveToPoint 和 curveToPoint 方法,实现了两种不同形状的绘制,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



NSBeizerPath苹果官方文档详见:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBezierPath_Class/Reference/Reference.html


一:绘制带箭头的矩形,如图所示



    NSRect theRect = NSInsetRect(self.boundskBeizerPathLineWidth * 2kArrowHeight);

    

    NSBezierPath *bezierPath = [NSBezierPath bezierPath];

    

    NSPoint arrowLeft = NSMakePoint(NSMidX(theRect) - kArrowWidth/2NSMaxY(theRect));

    NSPoint arrowRight = NSMakePoint(NSMidX(theRect) + kArrowWidth/2NSMaxY(theRect));

    NSPoint arrowTop = NSMakePoint(NSMidX(theRect), NSMaxY(self.bounds));


    //画成直角的矩形

    [bezierPath moveToPoint:NSMakePoint(kBeizerPathLineWidthkArrowHeight)];

    [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(kBeizerPathLineWidthkArrowHeight)];


    [bezierPath setLineWidth:kBeizerPathLineWidth];

    [[NSColor blackColorsetStroke];

    [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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值