【问题标题】:Can't get UIBezierPath to apply linecapstyle / linejoinstyle无法让 UIBezierPath 应用 linecapstyle / linejoinstyle
【发布时间】:2014-06-22 15:25:25
【问题描述】:

我正在尝试在我的 UIView 子类中绘制一个 V 形。出现 V 形,但我应用的线帽样式和线连接样式未反映在输出中。

 - (UIBezierPath *)chevron:(CGRect)frame
{
    UIBezierPath* bezierPath = [[UIBezierPath alloc]init];
    [bezierPath setLineJoinStyle:kCGLineJoinRound];
    [bezierPath setLineCapStyle:kCGLineCapRound];
    [bezierPath moveToPoint:CGPointMake(CGRectGetMinX(frame), CGRectGetMaxY(frame))];
    [bezierPath addLineToPoint:CGPointMake(CGRectGetMaxX(frame), CGRectGetMaxY(frame) * 0.5)];
    [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame))];

    return bezierPath;
}
-(void)drawRect:(CGRect)rect{
    [self.color setStroke];
    UIBezierPath *chevronPath = [self chevron:rect];
    [chevronPath setLineWidth:self.strokeWidth];
    [chevronPath stroke];
}

根据 Apple 的文档,他们说“在配置 Bezier 路径的几何形状和属性后,您可以使用描边和填充方法在当前图形上下文中绘制路径”,但这在这里不起作用 --- 我'已经尝试移动 setLineJoinStylesetLineCapStyle 语句(例如,在添加 LineToPoint 后,在 drawRect 内),似乎无论我调用它们多少次它都不起作用。任何想法出了什么问题?

【问题讨论】:

    标签: ios core-graphics uibezierpath


    【解决方案1】:

    您的代码正在应用这些样式,您只是看不到它们,因为您的 V 形一直被绘制到视图的边缘,然后被剪裁。要查看 chevron 的末端,请将您对 chevron 方法的调用更改为此,

    UIBezierPath *chevronPath = [self chevron:CGRectInset(rect, 10, 10)];
    

    10 点是否足够插入将取决于您的线有多宽,因此您可能需要增加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多