2017-08-28 8 views
0

こんにちは私は、常に変化する値を使って、行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

答えて

0

は、それはあなたが達成しようとしているもの、あなたの質問から明らかではないので、これは本当に答えではないが、それは私がコメントを入力したかった以上です。

最初に行うべきことは別々の責任であるため、上記のクラスCircleは、特定の構成の半円を描画する責任を負います。このクラスの外で、あなたはJSONは、あなたがしている値に基づいているものprogress決定するであろう他のコードを持っているでしょう

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() 
    } 
} 

次に、上記のコードに基づいて、例えば

、半円形の進捗ビュー受信する。

0

drawCircleCenteredAtVariavelが機能すると仮定して、lineWidth値が変更されるたびに再描画する必要があります。試してみよう

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

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