【问题标题】:UIBezierPaths Not Showing in CALayer drawLayer inContextUIBezierPaths未显示在CALayer drawLayer inContext中
【发布时间】:2013-07-23 10:02:50
【问题描述】:

我正在使用下面的代码在自定义 CALayer 类的 drawLayer 方法中绘制一条弧线,但没有显示任何内容:

(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{
    float r = self.bounds.size.width/2;

    CGContextClearRect(ctx, self.bounds); // clear layer
    CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);

    //CGContextFillRect(ctx, layer.bounds);

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:r startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:NO];

    CGContextAddPath(ctx, path.CGPath);
    CGContextStrokePath(ctx);
}

请注意,如果我取消注释 CGContextFillRect(ctx, layer.bounds) 行,则会正确呈现一个矩形。

【问题讨论】:

  • 缺少描边颜色?

标签: ios core-animation calayer quartz-graphics


【解决方案1】:

我看到的问题是你没有设置描边颜色(而是填充颜色)

CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);

当您配置 fill 颜色时,它用于 fill 路径。描边颜色也一样。


如果你只想描边或填充路径,那么你应该使用 either CGContextStrokePathCGContextFillPath 但如果你想两者都做,那么你应该使用 CGContextDrawPath kCGPathFillStroke 为“绘图模式”。

【讨论】:

    【解决方案2】:

    感谢你们俩。我最终决定在drawLayer方法外使用如下代码绘制圆弧:

    - (id) initWithLayer:(id)layer withRadius:(float)radius
    {
      self = [super initWithLayer:layer];
    
      // ...
    
      self.strokeColor = [UIColor whiteColor].CGColor;
      self.lineWidth = 10.0;
    
      return self;
    }
    
    - (void) update:(float)progress 
    {
        // ....
    
        UIBezierPath *arcPath = [UIBezierPath bezierPath];
        [arcPath addArcWithCenter:CGPointMake(r, r) radius:r  - self.lineWidth/2  startAngle:startAngle endAngle:nextAngle clockwise:YES];
        self.path = arcPath.CGPath;
    }
    

    【讨论】:

      【解决方案3】:

      看起来 CGPath 和 UIBezierPath 的所有弧线绘制方法在 64 位设备上都有一个错误,它们只有在顺时针参数设置为 YES 时才有效。我注意到您的非工作代码显示clockwise:NO,而工作代码显示clockwise:YES

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2014-04-05
        • 1970-01-01
        • 2013-11-21
        相关资源
        最近更新 更多