Trying to draw multiple circles but both circles connect with a line

时间:2015-04-23 05:25:47

标签: ios swift

I drew a circle but when I try to add another circle a line connects both circles.

import UIKit

class skills: UIView {

    override func drawRect(rect: CGRect)
    {
        var startAngle: Float = Float(2 * M_PI)
        var endAngle: Float = 0.0
        var endAngletwo: Float = 2.0

        // Drawing code
        // Set the radius
        let strokeWidth = 10.0
        let radius = CGFloat((CGFloat(self.frame.size.width) - CGFloat(strokeWidth)) / 20)


        // Get the context
        var context = UIGraphicsGetCurrentContext()



        **// Find the middle of the circle
        let center = CGPointMake(100, 200)

        let inside = CGPointMake(100,400)**

        // Set the stroke color
        CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)

        // Set the line width
        CGContextSetLineWidth(context, CGFloat(strokeWidth))

        // Set the fill color (if you are filling the circle)
        CGContextSetFillColorWithColor(context, UIColor.clearColor().CGColor)

        // Rotate the angles so that the inputted angles are intuitive like the clock face: the top is 0 (or 2π), the right is π/2, the bottom is π and the left is 3π/2.
        // In essence, this appears like a unit circle rotated π/2 anti clockwise.
        startAngle = startAngle - Float(M_PI_2)
        endAngle = endAngle - Float(M_PI_2)
        endAngletwo = endAngletwo - Float(M_2_PI)

        // Draw the arc around the circle
        CGContextAddArc(context, center.x, center.y, CGFloat(radius), CGFloat(startAngle), CGFloat(endAngle), 0)

        CGContextAddArc(context, inside.x, inside.y, CGFloat(radius), CGFloat(startAngle), CGFloat(endAngletwo),0)

        // Draw the arc
        CGContextDrawPath(context, kCGPathStroke) // or kCGPathFillStroke to fill and stroke the circle







    }










}

I think the problem is where the //Find the middle of the circle is because when Swift detects CGPointMake it will connect the two points? I think, i'm not too sure. How would I fix this?

1 个答案:

答案 0 :(得分:0)

来自tofile文档:

  

如果当前路径已包含子路径,Quartz会添加一行   将当前点连接到弧的起始点。如果   当前路径为空,Quartz创建一个带有的新子路径   起点设置为弧的起点。

因此你必须"搬到"首先是圈子的起点:

CGContextAddArc()

(如果您使用CGContextMoveToPoint(context, center.x + CGFloat(cos(startAngle)) * radius, center.y + CGFloat(sin(startAngle)) * radius) CGContextAddArc(context, center.x, center.y, CGFloat(radius), CGFloat(startAngle), CGFloat(endAngle), 0) CGContextMoveToPoint(context, inside.x + CGFloat(cos(startAngle)) * radius, inside.y + CGFloat(sin(startAngle)) * radius) CGContextAddArc(context, inside.x, inside.y, CGFloat(radius), CGFloat(startAngle), CGFloat(endAngletwo),0) 而不是CGFloat / CGFloat`混合使用,则需要更少的类型对话。)

或者使用Float绘制一个完整的圆圈。