我使用下面的代码将弧线绘制到自定义的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)
行的注释,则会正确地呈现一个矩形。
发布于 2013-07-25 09:37:58
我看到的问题是,您没有设置笔画颜色(而是设置填充颜色)。
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
配置填充颜色时,它用于填充路径。笔画的颜色也一样。
如果您只想笔画或者填充路径,那么您应该使用CGContextStrokePath
或CGContextFillPath
,但是如果您想两者都使用,那么您应该使用CGContextDrawPath
和kCGPathFillStroke
作为“绘图模式”。
发布于 2013-08-27 12:38:41
多亏了你们俩。最后,我决定使用以下代码在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;
}
发布于 2015-01-09 02:23:56
看起来,所有用于CGPath和UIBezierPath的弧线绘制方法在64位设备上都有错误,只有当顺时针参数设置为YES时,它们才能工作。我注意到您的非工作代码显示了clockwise:NO
,而工作代码有clockwise:YES
。
https://stackoverflow.com/questions/17807081
复制相似问题