简体   繁体   English

使用setNeedsDisplay更新UIView不起作用

[英]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(): 此代码位于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. 谢谢。

Every time your view controller appears and viewDidAppear() is called, you're creating a brand new ProgressCircle view and sending it to the back. 每次出现视图控制器并调用viewDidAppear() ,您都会创建一个全新的 ProgressCircle视图并将其发送到后面。 Unless you're doing it elsewhere, it doesn't look like you ever remove the ProgressCircle from the budgetDisplayView . 除非您在其他地方执行此操作,否则您似乎不会从budgetDisplayView删除ProgressCircle 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. 如果我是正确的,当应用程序从完全关闭打开, viewDidAppear()将被调用的第一次视图控制器和一个进度圆圈将被创建和你在观察中。 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! 下次显示视图控制器并再次调用viewDidAppear()时,会创建另一个进度循环并添加到上次创建的循环后面! 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) . 作为快速测试(但不是长期解决方案),请尝试评论该行: self.budgetDisplayView.sendSubviewToBack(circle) Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM