开发者社区> 问答> 正文

关于iphone使用drawRect画圆

我要在UIView中画一个简单的圆形。我不想用QuartzCore,能不能用drawRect实现?

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, rect);
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor greenColor] CGColor]));
    CGContextFillPath(ctx);
}

上述代码在设置[UIColor whiteColor]时就不工作。在设置了[UIColor whiteColor],视图就变成了透明的。

为什么设置白色但是视图变透明?怎么样画圈?

展开
收起
爵霸 2016-03-24 09:43:03 1902 0
1 条回答
写回答
取消 提交回答
  • greenColor 来自RGB颜色,但是whiteColor不是,所以你用错了。

    最好的方法是去掉:
    `
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor greenColor] CGColor]));
    `
    换成:
    `
    [[UIColor greenColor] set];
    `

    或者
    `
    [[UIColor whiteColor] set];
    `

    另外一点,如果你需要Core Graphics,代码如下:
    `
    CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
    `

    2019-07-17 19:11:58
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载