重新绘制绘制圆的值

时间:2017-08-28 19:11:50

标签: swift

嗨,我正在创建一个半圆,根据我给你的值总是变化来更新你的行widh。我想知道如何更新值以确定结束角度,并始终更新这些值。感谢

class Circle: UIView {  
//These 2 var (total dictionary and Secondary dictionary) have the number of the keys and the values ​​are always varying because they are obtained from json data
    var dictionaryTotal: [String:String] = ["a":"153.23", "b":"162.45", "c":"143.65", "d":"140.78", "e":"150.23"]

    var dictionarySecundario: [String:String] = ["w":"153.23", "l":"162.45", "v":"143.65"]

    var lineWidth: CGFloat = 25

    // circles radius
    var half = CGFloat.pi
    var quarter = CGFloat(3 * CGFloat.pi / 2)

    func drawCircleCenteredAtVariavel(center:CGPoint, withRadius radius:CGFloat) -> UIBezierPath{
        let circlePath = UIBezierPath(arcCenter: centerOfCircleView, radius: halfOfViewsSize, startAngle: half, endAngle: CGFloat(((CGFloat(self.dictionarySecundario.count) * CGFloat.pi) / CGFloat(self. dictionaryTotal.count)) + CGFloat.pi), clockwise: true)

        circlePath.lineWidth = lineWidth

        UIColor(red: 0, green: 0.88, blue: 0.70, alpha: 1).set()

        return circlePath
    }
    override func draw(_ rect: CGRect) {
        drawCircleCenteredAtVariavel(center: centerOfCircleView, withRadius: halfOfViewsSize).stroke()

    }

}

This is the picture of semicircle

2 个答案:

答案 0 :(得分:0)

从你的问题中不清楚你想要完成什么,所以这并不是真正的答案,但它比我想输入的评论更多。< / p>

您应该做的第一件事是分开的职责,因此上面的Circle类应该只负责为特定配置绘制半圆。

例如,基于上述代码的半圆形进度视图:

class CircleProgressView: UIView {
    var lineWidth: CGFloat = 25
    var barColor = UIColor(red: 0, green: 0.88, blue: 0.70, alpha: 1)
    var progress: Float = 0
    {
        didSet {
            progress = max(min(progress, 1.0), 0.0)
            setNeedsDisplay()
        }
    }

    func semicirclePath(at center: CGPoint, radius: CGFloat, percentage: Float) -> UIBezierPath {
        let endAngle = CGFloat.pi + (CGFloat(percentage) * CGFloat.pi)
        let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat.pi, endAngle: endAngle, clockwise: true)

        circlePath.lineWidth = lineWidth

        return circlePath
    }

    override func draw(_ rect: CGRect) {
        barColor.set()

        let center = CGPoint.init(x: bounds.midX, y: bounds.midY)
        let radius = max(bounds.width, bounds.height)/2 - lineWidth/2
        semicirclePath(at: center, radius: radius, percentage: progress).stroke()
    }
}

然后,在本课程之外,您将拥有其他代码,以确定progress基于您正在接收的JSON值。

答案 1 :(得分:0)

假设您的drawCircleCenteredAtVariavel有效,则应在每次更改lineWidth值时重绘。试试这个

var lineWidth: CGFloat = 25 {
    didSet {
        setNeedsDisplay()
    }
}

override func draw(_ rect: CGRect) {
    super.draw(rect)
    drawCircleCenteredAtVariavel(center: centerOfCircleView, withRadius: halfOfViewsSize).stroke()
}