2012-03-13 21 views
5

का उपयोग करके दो मंडलियों को चित्रित करना मैं नीचे की छवि के रूप में दूसरे के अंदर दो मंडलियों को खींचने की कोशिश कर रहा हूं।क्वार्ट्ज CGContextFillEllipseInRect

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

} 

उत्तर

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

+0

हां, मैं इसे भूल गया। यदि आप दोनों भरना और स्ट्रोक करना चाहते हैं तो आप 'केसीजीपीएथफिलस्ट्रोक' का भी उपयोग कर सकते हैं। – sch

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

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() 
} 
संबंधित मुद्दे