使用Quartz CGContextFillEllipseInRect绘制两个圆

che*_*ewy 5 iphone objective-c quartz-graphics ios

我试图在另一个内部绘制两个圆圈,如下图所示.

在此输入图像描述

我设法很好地绘制了一个圆圈(外部圆圈),但我不确定如何在顶部添加第二个圆圈,以及如何将其居中.

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

}
Run Code Online (Sandbox Code Playgroud)

sch*_*sch 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();
Run Code Online (Sandbox Code Playgroud)


Jul*_*cia 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();
Run Code Online (Sandbox Code Playgroud)