簡體   English   中英

使用setNeedsDisplay更新UIView不起作用

[英]Updating UIView with setNeedsDisplay does not work

我的應用中有一個循環進度條,顯示他們花費的用戶預算的百分比。 每次用戶切換到具有圓形進度條的視圖時,進度條都應更新。 每次用戶打開應用程序時,它也應該更新。

目前,由於某些原因我無法理解,它只會在應用程序打開時更新(從多任務處理中關閉),如果用戶切換到該視圖(它應該),它將永遠不會更新。 我已完成大量研究,但未能找到解決方案。

以下是創建圓圈的代碼:

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

這是我用來(嘗試)更新進度圈的代碼。 此代碼位於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()
}

我真的希望有人有解決方案,因為我已經有這個問題大約一個星期了。 謝謝。

每次出現視圖控制器並調用viewDidAppear() ,您都會創建一個全新的 ProgressCircle視圖並將其發送到后面。 除非您在其他地方執行此操作,否則您似乎不會從budgetDisplayView刪除ProgressCircle 我對嗎?

如果我是正確的,當應用程序從完全關閉打開, viewDidAppear()將被調用的第一次視圖控制器和一個進度圓圈將被創建和你在觀察中。 下次顯示視圖控制器並再次調用viewDidAppear()時,會創建另一個進度循環並添加到上次創建的循環后面! 每次都會發生同樣的事情。 我估計這就是你無法看到它的原因。 作為快速測試(但不是長期解決方案),請嘗試評論該行: self.budgetDisplayView.sendSubviewToBack(circle) 希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM