0

I am using the code below to draw an arc into the drawLayer method of a custom CALayer class but nothing is displayed:

(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);
}

Note that if I uncomment the CGContextFillRect(ctx, layer.bounds) line, a rectangle is properly rendered.

STW
  • 44,917
  • 17
  • 105
  • 161
Karthun
  • 1
  • 3

3 Answers3

0

The problem I can see is that you don't set a stroke color (but a fill color)

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

When you configure a fill color it is used to fill the path. Same for a stroke color.


If you want to only want to either stroke or fill the path then you should use either CGContextStrokePath or CGContextFillPath but if you want to do both then you should use CGContextDrawPath with kCGPathFillStroke as the "drawing mode".

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
0

Thanks to both of you. I finally decided to draw the arc outside the drawLayer method using the following code:

- (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;
}
Karthun
  • 1
  • 3
0

It looks like all the arc drawing methods for both CGPath and UIBezierPath have a bug on 64 bit devices where they only work if the clockwise parameter is set to YES. I notice that your non-working code shows clockwise:NO and the working code has clockwise:YES.

Duncan C
  • 128,072
  • 22
  • 173
  • 272