Ios UIBezierPath未显示在CALayer drawLayer InText中
我正在使用下面的代码在自定义Ios UIBezierPath未显示在CALayer drawLayer InText中,ios,core-animation,calayer,quartz-graphics,Ios,Core Animation,Calayer,Quartz Graphics,我正在使用下面的代码在自定义CALayer类的drawLayer方法中绘制圆弧,但未显示任何内容: (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { float r = self.bounds.size.width/2; CGContextClearRect(ctx, self.bounds); // clear layer CGContextSetFillColorWithColor(ct
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)
行,矩形将正确渲染。我看到的问题是您没有设置笔划颜色(而是填充颜色)
配置填充颜色时,它用于填充路径。笔划颜色也一样
如果您只想绘制或填充路径,则应使用
CGContextStrokePath
或CGContextFillPath
,但如果您想同时绘制或填充路径,则应使用CGContextDrawPath
和kCGPathFillStroke
作为“绘图模式”。多亏了你们两位。我最终决定使用以下代码在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;
}
看起来CGPath和UIBezierPath的所有圆弧绘制方法在64位设备上都有一个bug,只有当顺时针参数设置为“是”时,它们才能工作。我注意到您的非工作代码显示
顺时针:否
,并且工作代码有顺时针:是
缺少笔划颜色?
- (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;
}