Progres酒吧套装

时间:2020-10-16 11:06:31

标签: ios swift iphone count progress-bar

我有两个数字。当前得分和maxScore。例如0/1000。 我有几个级别。每个级别都有自己的经验。 1000、2000、3000。当用户的得分从0到1000时,进度栏将从开始到结束填充。但是,当用户获得1000分时,他现在应该获得2000分。然后,进度条从中间开始填充,而不是从开始填充,就像从0到1000一样。

setInterval

2 个答案:

答案 0 :(得分:2)

好的-您需要跟踪另一条信息...我们称其为“基本分数”。

用户从零开始,目标是1000。随着他获得积分,您将进度设置为1000的百分比:

// supposing the curentScore is 900, maxScore is 1000
progress.progress = Float(currentScore)/Float(maxScore)

栏已填充90%。

当用户达到1000时,您将新目标设置为2000,并希望以currentScore到{的百分比来跟踪进度 不是 {1}}。相反,您想跟踪从 上一个 maxScore maxScore的进度

所以,像这样:

maxScore

因此,func setProgress() -> Void { let currentScore = getScore() let maxScore = getMaxScore() // for example, if user is at: // Level 1 == 0 // Level 2 == 1000 // Level 3 == 2000 let levelBase = getLevelBaseScore() let curVal = Float(currentScore - levelBase) let maxVal = Float(maxScore - levelBase) progressBar.progress = curVal / maxVal } 应该返回上一级的最高分数。

示例:

如果用户的等级为“ 1级”,并且他的当前得分为750:

getLevelBaseScore()

如果用户的等级为“ 2级”,并且他的当前分数是1100:

    let currentScore = getScore()           // returns 750
    let maxScore     = getMaxScore()        // returns 1000
    
    let levelBase = getLevelBaseScore()     // returns Zero for Level 1
    
    let curVal = Float(750 - 0)             // == 750
    let maxVal = Float(1000 - 0)            // == 1000
    
    // 750 / 1000 == 0.75 or 75% of the bar
    progressBar.progress = curVal / maxVal

如果用户处于“ 2级”,并且他的当前分数是1600:

    let currentScore = getScore()           // returns 1100
    let maxScore     = getMaxScore()        // returns 2000
    
    let levelBase = getLevelBaseScore()     // returns 1000 for Level 2
    
    let curVal = Float(1100 - 1000)         // == 100
    let maxVal = Float(2000 - 1000)         // == 1000
    
    // 100 / 1000 == 0.10 or 10% of the bar
    progressBar.progress = curVal / maxVal

如果用户的等级为“ 3级”,并且他的当前分数是2250:

    let currentScore = getScore()           // returns 1600
    let maxScore     = getMaxScore()        // returns 2000
    
    let levelBase = getLevelBaseScore()     // returns 1000 for Level 2

    let curVal = Float(1600 - 1000)         // == 600
    let maxVal = Float(2000 - 1000)         // == 1000
    
    // 600 / 1000 == 0.60 or 60% of the bar
    progressBar.progress = curVal / maxVal

答案 1 :(得分:1)

let currentScore = 25 
let maxScore     = 50 

@IBOutlet weak var progress: UIProgressView!

progress.progress = Float(currentScore)/Float(maxScore)

我希望它能正常工作