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],视图就变成了透明的。
为什么设置白色但是视图变透明?怎么样画圈?
答
greenColor 来自RGB颜色,但是whiteColor不是,所以你用错了。
最好的方法是去掉: CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor greenColor] CGColor]));
换成:
[[UIColor greenColor] set];
或者
[[UIColor whiteColor] set];
另外一点,如果你需要Core Graphics,代码如下:
CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);