2012-03-13 193 views
5

我试图在下面的图像中绘制两个圆圈之一。使用Quartz绘制两个圆圈CGContextFillEllipseInRect

enter image description here

我已成功地画出一个圆(外一种)很好,但我不知道如何添加在最上层的第二圆,以及如何居中。

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 4.0); 
    CGContextSetStrokeColorWithColor(context, 
            [UIColor whiteColor].CGColor); 
    // 
    UIColor *theFillColor = UIColorFromRGB(0x6c83a6); 
    CGContextSetFillColor(context, CGColorGetComponents(theFillColor.CGColor)); 

    CGRect rectangle = CGRectMake(5.0,5.0,rect.size.width-10.0,rect.size.height-10.0); 


    CGContextAddEllipseInRect(context, rectangle); 
    CGContextStrokePath(context); 
    CGContextFillEllipseInRect(context, rectangle); 
    UIGraphicsEndImageContext(); 
    // 
    // INSIDE ? 
    // 

} 

回答

14
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, 4.0); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
UIColor *theFillColor = UIColorFromRGB(0x6c83a6); 
CGContextSetFillColorWithColor(context, theFillColor.CGColor); 

CGRect rectangle = CGRectMake(5.0,5.0,rect.size.width-10.0,rect.size.height-10.0); 
CGContextBeginPath(context); 
CGContextAddEllipseInRect(context, rectangle); 
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill 

CGRect smallRect = CGRectInset(rectangle, 40, 40); // change 40 with the desired value 
// You may change the fill and stroke here before drawing the circle 
CGContextBeginPath(context); 
CGContextAddEllipseInRect(context, smallRect); 
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill 

UIGraphicsEndImageContext(); 
+0

嘿SCH,谢谢你的回答,你应该在上面的答案更改 “CGContextDrawPath(背景下,kCGPathFill);”在这两种情况下,但其他则表明它工作正常。 – chewy 2012-03-13 12:54:24

+0

是的,我忘了。如果你想填充和描边,你也可以使用'kCGPathFillStroke'。 – sch 2012-03-13 12:58:20

5

/* 以前的解决方案,但有些简单的类似。仅为了清晰起见而定义的半径和起始端角度变量。 */

#define PI 3.14285714285714 
float radius1 = 80; 
float radius2 = 30; 
float startAngle = 0; 
float endAngle = endAngle = PI*2; 
CGPoint position = CGPointMake(100,100); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, 4.0); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
UIColor *theFillColor = UIColorFromRGB(0x6c83a6); 
CGContextSetFillColorWithColor(context, theFillColor.CGColor); 

CGContextBeginPath(context); 
CGContextAddArc(ctx, position.x, position.y, radius1, startAngle, endAngle, 1); 
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill 

// You may change the fill and stroke here before drawing the circle 
CGContextBeginPath(context); 
CGContextAddArc(ctx, position.x, position.y, radius2, startAngle, endAngle, 1); 
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill 

UIGraphicsEndImageContext(); 
+1

你可以'M_PI'定义 – graver 2014-05-29 14:44:07

0

斯威夫特

func drawRect(rect: CGRect) { 
let context: CGContextRef = UIGraphicsGetCurrentContext()! 
CGContextSetLineWidth(context, 4.0) 
CGContextSetStrokeColorWithColor(context, UIColor(hue: 0, saturation: 0, brightness: 94, alpha: 242).CGColor) 

let theFillColor: UIColor = UIColor(hue: 0, saturation: 100, brightness: 80, alpha: 204) 
CGContextSetFillColorWithColor(context, theFillColor.CGColor) 

let rectangle: CGRect = CGRectMake(5.0, 5.0, rect.size.width-10.0, rect.size.height-10.0) 
CGContextBeginPath(context) 
CGContextAddEllipseInRect(context, rectangle) 
CGContextDrawPath(context, CGPathDrawingMode.FillStroke) 

let smallRect: CGRect = CGRectInset(rectangle, 40, 40) 

CGContextBeginPath(context) 
CGContextAddEllipseInRect(context, smallRect) 
CGContextDrawPath(context, CGPathDrawingMode.FillStroke) 
UIGraphicsEndImageContext() 
}