I have a circular progress bar in my app and it seems to be working great. However, for some reason I can't get the progress bar to shrink.
Basically the function of it is to display the percentage of a user's budget that they have spent. However the user can also add credit transactions, which of course you would expect to decrease the progress bar since they have increased their total budget. It doesn't seem to do this however. I also need it to reset to 0% when the budget is reset, which doesn't work.
Here is the code for the progress circle:
var progress: CGFloat = 0
class ProgressCircle: UIView {
override func drawRect(rect: CGRect) {
var ctx = UIGraphicsGetCurrentContext()
var innerRadiusRatio: CGFloat = 0.6
var path: CGMutablePathRef = CGPathCreateMutable()
var startAngle: CGFloat = CGFloat(-M_PI_2)
var endAngle: CGFloat = CGFloat(-M_PI_2) + min(1.0, progress) * CGFloat(M_PI * 2)
var outerRadius: CGFloat = CGRectGetWidth(self.bounds) * 0.5 - 1.0
var innerRadius: CGFloat = outerRadius * innerRadiusRatio
var center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))
CGPathAddArc(path, nil, center.x, center.y, innerRadius, startAngle, endAngle, false)
CGPathAddArc(path, nil, center.x, center.y, outerRadius, endAngle, startAngle, true)
CGPathCloseSubpath(path)
CGContextAddPath(ctx, path)
CGContextSaveGState(ctx)
CGContextClip(ctx)
CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFill").CGImage)
CGContextRestoreGState(ctx)
}
This is how I am currently trying to reset it:
progress = 0.00
This is how the progress is calculated:
percent = 100*totalSpendingsCounter/(currencyDouble + totalCreditCounter)
let nf = NSNumberFormatter()
nf.numberStyle = .DecimalStyle
if percent > 100 {
percentageDisplay.text = "100%"
} else {
var percentString = nf.stringFromNumber(percent) + "%"
percentageDisplay.text = percentString
}
progress = CGFloat(percent/100)
Any ideas?
First, progress should be a property of a ProgressCircle, not a global variable. Second, it needs to be marked as needing a redisplay when you set it:
class ProgressCircle: UIView {
var progress: CGFloat = 0 {
didSet {
setNeedsDisplay()
}
}
(The rest of your code...)