【问题标题】:how to fill an arc sector properly NSBezierPath, Objective-C如何正确填充弧形扇区 NSBezierPath,Objective-C
【发布时间】:2012-12-13 17:04:57
【问题描述】:

在我的 mac os x 计时器应用程序中,我想用绿色、黄色、橙色和红色标记时钟区域。请参阅下面的屏幕截图。我想用透明的灰色填充经过的时间。

但是,正如您在屏幕截图中看到的那样,仅填充了圆弧段。但我希望整个部门都被填满。我唯一能做的就是[thePath fill];

与往常一样,我认为我做错了什么。但是什么?


呼叫

[self drawTheArcWithColor:path1  :liveAngle1  :[NSColor greenColor ]  :lineTheWidth];

方法

- (void) drawTheArcWithColor:(NSBezierPath*) thePath :(CGFloat) angle :(NSColor*) theColor :(CGFloat) line {

    [thePath setLineWidth:line];
    [thePath appendBezierPathWithArcWithCenter:centerPoint  radius:2+circleHeight/2 startAngle:angle endAngle:angle+90.0f];

    [theColor setStroke];
    [[NSColor grayColor] setFill];
    [thePath fill];
    [thePath stroke];

}

【问题讨论】:

  • 你真的应该接受 Objective-C 方法名...
  • 我没明白你的意思?是这个问题吗?
  • 你能告诉我们你是如何创建和填充路径的其余部分的吗?
  • 您的方法名为-drawTheArcWithColor::::。它应该命名为-drawArc:angle:color:lineWidth: 或类似名称。
  • Apple 文档说这不是强制性的。我为什么要用这么短的方法?

标签: objective-c fill nsbezierpath sector


【解决方案1】:

你需要从中心点开始路径,然后添加圆弧段(隐式添加从中心点到圆弧段起点的直线)最后关闭路径(从终点创建一条隐含的直线)圆弧段到中心点)。

- (void) drawTheArcWithColor:(NSBezierPath*) thePath :(CGFloat) angle :(NSColor*) theColor :(CGFloat) line {

   [thePath setLineWidth:line];
   [thePath moveToPoint:centerPoint];
   [thePath appendBezierPathWithArcWithCenter:centerPoint  radius:2+circleHeight/2 startAngle:angle endAngle:angle+90.0f];
   [thePath closePath];

   [theColor setStroke];
   [[NSColor grayColor] setFill];
   [thePath fill];
   [thePath stroke];

}

【讨论】:

  • 啊哈,我明白了,我必须画出扇区。因为我刚刚创建了弧线,所以只填充了部分:) 我可以让从中心到开始/结束的两条线不可见吗?
  • 很好的提示,非常感谢。知道如何使填充透明吗?
  • 要使用部分透明的颜色填充路径,请使用颜色的 alpha 组件:[[NSColor colorWithDeviceRed: 0.1f green: 1.0f blue: 0.1f alpha: 0.4f] setFill];
  • 所有问题都解决了,非常感谢大家。来自瑞士的问候,罗纳德霍夫曼。
猜你喜欢
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 2012-04-17
  • 2013-11-25
  • 2015-04-01
  • 1970-01-01
相关资源
最近更新 更多