Iphone 使用石英CGContextFilllelipseInrect绘制两个圆

Iphone 使用石英CGContextFilllelipseInrect绘制两个圆,iphone,ios,objective-c,quartz-graphics,Iphone,Ios,Objective C,Quartz Graphics,我试图画两个圆圈,一个在另一个里面,如下图所示 我已经很好地画了一个圆(外圆),但我不知道如何在上面添加第二个圆,以及如何将其居中 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 4.0); CGContextSetStrokeColorWithColor(context,

我试图画两个圆圈,一个在另一个里面,如下图所示

我已经很好地画了一个圆(外圆),但我不知道如何在上面添加第二个圆,以及如何将其居中

- (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 ?
    //

}
/* 与前面的解决方案类似,但稍微简单一些。 半径和起点-终点角度变量仅为清晰起见而定义。 */

迅捷的

Swift 4.0

    let context: CGContext = UIGraphicsGetCurrentContext()!
    context.setLineWidth(4.0)
    context.setStrokeColor(UIColor(hue: 0, saturation: 0, brightness: 94, alpha: 242).cgColor)

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

    let rectangle: CGRect = CGRect(x: 5.0, y: 5.0, width: rect.size.width-10.0, height: rect.size.height-10.0)
    context.beginPath()
    context.addEllipse(in: rectangle)
    context.drawPath(using:CGPathDrawingMode.fillStroke)

    let smallRect: CGRect = rectangle.insetBy(dx: 40, dy: 40)

    context.beginPath()
    context.addEllipse(in: smallRect)
    context.drawPath(using: CGPathDrawingMode.fillStroke)
    UIGraphicsEndImageContext()

嘿,sch,谢谢你的回答,你应该在上面的答案“CGContextDrawPath(context,kCGPathFill);”中对这两种情况进行更改,但除此之外效果很好。是的,我忘了。如果要同时填充和笔划,也可以使用
kCGPathFillStroke
。您可以定义
M_PI
#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();
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()
}
    let context: CGContext = UIGraphicsGetCurrentContext()!
    context.setLineWidth(4.0)
    context.setStrokeColor(UIColor(hue: 0, saturation: 0, brightness: 94, alpha: 242).cgColor)

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

    let rectangle: CGRect = CGRect(x: 5.0, y: 5.0, width: rect.size.width-10.0, height: rect.size.height-10.0)
    context.beginPath()
    context.addEllipse(in: rectangle)
    context.drawPath(using:CGPathDrawingMode.fillStroke)

    let smallRect: CGRect = rectangle.insetBy(dx: 40, dy: 40)

    context.beginPath()
    context.addEllipse(in: smallRect)
    context.drawPath(using: CGPathDrawingMode.fillStroke)
    UIGraphicsEndImageContext()