2016-08-09 34 views
1

ich diese Größen haben, die ich mit einen Kreis zeichnen wollen (Breite und Höhe sind gleich offensichtlich)CGContextAddArc mehrere Kreis in verschiedenen Größen zeichnen

205.0 
218.0 
245.0 
257.0 
310.0 
510.0 

Als ich meinen Kreis auf 205,5 seinem feinen ziehen, seine ein perfekter Kreis. Aber wenn ich mit 218 anfange, wird der Kreis ein wenig abgeschnitten und er wird immer mehr abgeschnitten, wenn ich versuche, einen größeren Kreis zu bilden. Meine Frage ist, wie kreiere ich einen Kreis, der perfekt ist, egal welche Größe?

Hier ist mein Code:

func drawCircle() 
{ 
    // Get the Graphics Context 
    let context = UIGraphicsGetCurrentContext(); 

    // Set the circle outerline-width 
    CGContextSetLineWidth(context, 5.0); 

    // Set the circle outerline-colour 
    UIColor.redColor().set() 

    // Create Circle 
    CGContextAddArc(context, (round(frame.size.width))/2, round(frame.size.height)/2, (round(frame.size.width) - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1) 

    // Draw 
    CGContextStrokePath(context); 
} 

frame.size.width und frame.size.height sind die Zahlen von 205, 218 und so weiter. Ich werde eine Objective-C-Antwort akzeptieren.

+2

Was meinen Sie mit "abgeschnitten"? Es zieht nicht den ganzen Weg um den Kreis? Oder könnte es sein, dass es über den Rand der Containeransicht hinausgeht oder so? –

Antwort

3

Wenn Sie einen Kreis zeichnen möchten, use CGContext.addEllipse(in:) a.k.a. CGContextAddEllipseInRect anstelle von CGContextAddArc.

// swift 3: 
let context = UIGraphicsGetCurrentContext()! 
context.setLineWidth(5.0) 
UIColor.red.set() 

context.addEllipse(in: frame) // <-- 

context.strokePath() 


// swift 2: 
let context = UIGraphicsGetCurrentContext() 
CGContextSetLineWidth(context, 5.0) 
UIColor.redColor().set() 

CGContextAddEllipseInRect(context, frame) // <-- 

CGContextStrokePath(context) 
0

Der Blick Sie diese in Bedürfnisse ziehen groß genug sein, alle diese Kreise zu zeichnen in (so 510+ x 510+). Sie könnten clipsToBounds = false einstellen, um es zu umgehen, aber das ist normalerweise keine gute Idee.

UIBezierPath hat einen einfachen Initialisierer für Kreispfade. Sie können damit Ihre Kreise zeichnen und einen Radius angeben.

// Use the center of the bounds of the view so we draw circles centered in our view. 
// If you use self.center, this will be in the parent view's coordinate system and your circles could be off center. 
CGPoint centerOfBounds = CGPointMake((self.bounds.origin.x + self.bounds.size.width)/2, (self.bounds.origin.y + self.bounds.size.height)/2); 

// The startAngle, endAngle and clockwise don't matter much here since we are drawing a full circle. 
// The only requirement is the start and end angle MUST be 2*PI apart. 
UIBezierPath *two05 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:205.0/2, startAngle:0 endAngle:2*M_PI clockwise:false]; 
UIBezierPath *two18 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:218.0/2, startAngle:0 endAngle:2*M_PI clockwise:false]; 
UIBezierPath *two45 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:245.0/2, startAngle:0 endAngle:2*M_PI clockwise:false]; 
UIBezierPath *two57 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:257.0/2, startAngle:0 endAngle:2*M_PI clockwise:false]; 
UIBezierPath *three10 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:310.0/2, startAngle:0 endAngle:2*M_PI clockwise:false]; 
UIBezierPath *five10 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:510.0/2, startAngle:0 endAngle:2*M_PI clockwise:false]; 

// Set the color, line widths on the path, and call stroke on each bezier path to draw the circles 
[[UIColor blueColor] setStroke]; 
two05.lineWidth = 1; 
[two05 stroke]; 
// Reapeat for other paths...