Search code examples
iosuiviewswiftdrawrectsetneedsdisplay

Updating UIView with setNeedsDisplay does not work


I have a circular progress bar in my app that displays the percentage of a users budget that they have spent. The progress bar should update every time that the user switches to the view with the circular progress bar on it. It should also update each time the user opens the app.

At the moment, for some reason that I fail to understand, it will only update when the app is opened (from being closed in multitasking) and will never update if the user switches to that view (which it should). I've done loads of research but haven't been able to find a solution.

Here is the code that creates the 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))

    //Code for background circle

    var context: CGContextRef = UIGraphicsGetCurrentContext()
    colourTheme = NSUserDefaults.standardUserDefaults().integerForKey("themeSettings")
    if colourTheme == 1 {
        CGContextSetFillColorWithColor(context, (UIColorFromRGB(0xEAEAEA)).CGColor)
    } else {
        CGContextSetFillColorWithColor(context, (UIColor.darkGrayColor()).CGColor)
    }

    var circleSize: CGFloat = 0
    switch height {
    case 480, 568:
        circleSize = 171
    default:
        circleSize = 250
    }

    var rectangle: CGRect = CGRectMake(0, 0, circleSize, circleSize)
    CGContextBeginPath(context)
    CGContextAddEllipseInRect(context, rectangle)
    CGContextDrawPath(context, kCGPathFill)

    UIGraphicsEndImageContext()

    if colourTheme == 1 {
        CGContextSetFillColorWithColor(context, (UIColor.darkGrayColor()).CGColor)
    } else {
        CGContextSetFillColorWithColor(context, (UIColorFromRGB(0xEAEAEA)).CGColor)
    }

    var circleXY: CGFloat = 0
    var circleWH: CGFloat = 0
    switch height {
    case 480, 568:
        circleXY = 35.95
        circleWH = 99.1
    default:
        circleXY = 52
        circleWH = 146
    }
    var rectangleSmall: CGRect = CGRectMake(circleXY, circleXY, circleWH, circleWH) //102.6
    CGContextBeginPath(context)
    CGContextAddEllipseInRect(context, rectangleSmall)
    CGContextDrawPath(context, kCGPathFill)

    UIGraphicsEndImageContext()

    //Code for progress radius

    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)
    if percent > 100 {
        CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFillOver").CGImage)
    } else {
        CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFill").CGImage)
    }
    CGContextRestoreGState(ctx)
}

And here is the code that I am using to (try) to update the progress circle. This code is in viewDidAppear():

override func viewDidAppear(animated: Bool) {
    //Show set up screen if user hasn't used AffordIt before.

    userReturned = NSUserDefaults.standardUserDefaults().boolForKey("userReturnedCheck")
    if userReturned == false {
        self.performSegueWithIdentifier("setupView", sender: self)
    }

    //Add circular progress bar to budget display view.

    var circleSize: CGFloat = 0
    var yCoord: CGFloat = 0
    switch height {
    case 480, 568:
        circleSize = 171
        yCoord = 249
    case 667:
        circleSize = 250
        yCoord = 249
    default:
        circleSize = 250
        yCoord = 279
    }

    progress = CGFloat(percent/100)
    var circleFrame = CGRect(x: (budgetDisplayView.bounds.width/2)-(circleSize/2), y: yCoord, width: circleSize, height: circleSize)
    var circle = ProgressCircle(frame: circleFrame)
    self.budgetDisplayView.addSubview(circle)
    self.budgetDisplayView.sendSubviewToBack(circle)
    circle.setNeedsDisplay()
    view.setNeedsDisplay()

    circle.backgroundColor = UIColor.clearColor()
}

I really hope someone has a solution because I have had this problem for about a week now. Thanks.


Solution

  • Every time your view controller appears and viewDidAppear() is called, you're creating a brand new ProgressCircle view and sending it to the back. Unless you're doing it elsewhere, it doesn't look like you ever remove the ProgressCircle from the budgetDisplayView. Am I right?

    If I am right, when the app is opened from being completely closed, viewDidAppear() will be called for the first time on that view controller and a single progress circle will be created and shown as you're observing. The next time that view controller is shown, and viewDidAppear() is called again, another progress circle is created and added behind the one created last time! The same thing happens every subsequent time. I reckon that's why you can't see it. As a quick test (but not a long term solution), try commenting the line: self.budgetDisplayView.sendSubviewToBack(circle). Hope that helps.