首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIBezierPaths没有在CALayer drawLayer inContext中显示

UIBezierPaths没有在CALayer drawLayer inContext中显示
EN

Stack Overflow用户
提问于 2013-07-23 10:02:50
回答 3查看 2K关注 0票数 0

我使用下面的代码将弧线绘制到自定义的drawLayer方法中,但是没有显示:

代码语言:javascript
运行
复制
(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)行的注释,则会正确地呈现一个矩形。

EN

回答 3

Stack Overflow用户

发布于 2013-07-25 09:37:58

我看到的问题是,您没有设置笔画颜色(而是设置填充颜色)。

代码语言:javascript
运行
复制
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);

配置填充颜色时,它用于填充路径。笔画的颜色也一样。

如果您只想笔画或者填充路径,那么您应该使用CGContextStrokePathCGContextFillPath,但是如果您想两者都使用,那么您应该使用CGContextDrawPathkCGPathFillStroke作为“绘图模式”。

票数 0
EN

Stack Overflow用户

发布于 2013-08-27 12:38:41

多亏了你们俩。最后,我决定使用以下代码在drawLayer方法之外绘制弧线:

代码语言:javascript
运行
复制
- (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;
}
票数 0
EN

Stack Overflow用户

发布于 2015-01-09 02:23:56

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17807081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档