рисование двух кругов с помощью Quartz CGContextFillEllipseInRect - PullRequest
5 голосов
/ 13 марта 2012

Я пытаюсь нарисовать два круга один внутри другого, как показано на рисунке ниже.

enter image description here

Мне удалось красиво нарисовать один круг (внешний), ноЯ не уверен, как добавить 2-й круг сверху и как его отцентрировать.

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

}

Ответы [ 4 ]

14 голосов
/ 13 марта 2012
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();
5 голосов
/ 12 октября 2012

/ * Аналогично предыдущему решению, но несколько проще. переменные радиуса и начального угла определены только для ясности. * /

#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 голос
/ 21 июля 2016

Свифт

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()
}
0 голосов
/ 12 июня 2018

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()
...