2012-03-13 59 views
5

Tôi đang cố gắng vẽ hai vòng tròn một bên trong hình tròn như hình bên dưới.vẽ hai vòng tròn bằng cách sử dụng Quartz CGContextFillEllipseInRect

enter image description here

Tôi đã cố gắng vẽ một vòng tròn (một trong những bên ngoài) độc đáo, nhưng tôi không chắc chắn làm thế nào để thêm vòng tròn thứ 2 on-top, và làm thế nào để tập trung nó.

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

} 

Trả lời

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

Hey sch, cảm ơn bạn cho câu trả lời, bạn nên thay đổi trong câu trả lời ở trên "CGContextDrawPath (bối cảnh, kCGPathFill);" trên cả hai trường hợp, nhưng khác thì nó hoạt động tốt. – chewy

+0

Vâng, tôi quên điều đó. Bạn cũng có thể sử dụng 'kCGPathFillStroke' nếu bạn muốn cả điền và đột quỵ. – sch

5

/* Tương tự như các giải pháp trước nhưng hơi đơn giản. bán kính và các biến góc bắt đầu được xác định để chỉ rõ ràng. */

#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

bạn có thể định nghĩa 'M_PI' – graver

0

Swift

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() 
} 
Các vấn đề liên quan