objective-c - 很难理解 NSBezierPath 的 curveToPoint : method

标签 objective-c cocoa macos quartz-graphics

我正试图掌握在 Cocoa 中绘制(相当基本的)形状。我知道如何创建具有直边的路径(duh!),但是当谈到做曲线时,我就是无法理解什么输入会产生什么形状的曲线。具体来说,我不知道该方法的 controlPoint1:controlPoint2: 参数如何影响形状。

我正在尝试近似 Google Chrome 中标签的形状:

Google Chrome Tab

我使用的代码是:

-(void)drawRect:(NSRect)dirtyRect {
    NSSize size = [self bounds].size;

    CGFloat height = size.height;
    CGFloat width = size.width;

    NSBezierPath *path = [NSBezierPath bezierPath];
    [path setLineWidth:1];

    [path moveToPoint:NSMakePoint(0, 0)];

    [path curveToPoint:NSMakePoint(width * 0.1, height)
         controlPoint1:NSMakePoint(width * 0.05, height)
         controlPoint2:NSMakePoint(width * 0.03, height * 0.05)];

    [path lineToPoint:NSMakePoint(width * 0.9, height)];

    [path curveToPoint:NSMakePoint(width, 0)
         controlPoint1:NSMakePoint(width * 0.95, height)
         controlPoint2:NSMakePoint(width * 0.97, height * 0.05)];

    [path closePath];

    [[NSColor colorWithCalibratedWhite:0.98 alpha:1] set];
    [path fill];

    [[NSColor colorWithCalibratedWhite:0.6 alpha:1] set];
    [path stroke];
}

我失败得很厉害。

看,这就是我们不能拥有美好事物的原因:(

My Attempt

任何人都可以给我一些关于在绘制曲线时如何思考的指示吗?绘制这条路径的示例也很棒,但实际上只是理解 curveToPoint:controlPoint1:controlPoint2: 的这些输入让我退缩了。

更新 |感谢@Ahruman 的回答,我终于开始成形了。它不是 100%(缺少底角的曲线,但至少现在它实际上是一个对称的形状:)

Getting there

最佳答案

当前绘图点(隐式)和控制点 1 之间的线是曲线起点处的切线。控制点 2 和“到”点之间的线是曲线末端的切线。这些对应于您在具有贝塞尔路径的任何矢量绘图应用程序中看到的两个切线控件的末端。如果您还没有使用过,Inkscape 是免费的。

关于objective-c - 很难理解 NSBezierPath 的 curveToPoint : method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4499728/

相关文章:

iphone - 如何实现 "What' s Pic"iPhone 应用程序像砖 block 动画?

objective-c - 沙盒、登录项目和启动应用程序

objective-c - NSTokenField 不允许我输入除 tokenField :completionsForSubstring:. 之外的其他字符串 .. 返回

macos - nextKeyView 切换到 NSBrowser 并在工具栏中搜索 View

ios - objective c 从 url 请求中解析 json

ios - 如何更改特定单词的 UILabel 文本颜色变化?

objective-c - 如何在 Cocoa/Objective-C 中使用应用程序委托(delegate)?

iphone - 什么事件会重置 backgroundTimeRemaining?

java - 添加 Swing 菜单栏时出现一堆错误

macos - 使用捆绑安装程序安装 AWS CLI(Linux、OS X 或 Unix)