2017-08-28 55 views
0

嗨我正在创建一个半圆形,根据我给出的值总是变化的值来更新您的线宽。我想知道如何更新这些值以确定结束角度,并且这些值始终更新。由于重新加载绘制圆的值

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上面的类应该只负责绘制特定配置的半圆。

例如,一个半圆形的进步视图根据您以上代码:

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

然后,这个类之外,你就会有其他的代码,将确定的JSON值你在做什么progress基于接收。

0

假设您的drawCircleCenteredAtVariavel工作,只要lineWidth值发生更改,您应该重绘。试试这个

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

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