ios - UIProgressView 作为健康栏并在按下按钮时更新。 swift

标签 ios swift uiprogressview

所以我开始制作游戏,我想将进度条用作许多不同事物的健康条,但我不明白为什么它根本不更新。但是,它确实在 viewdidload 函数中进行了更新。这是我正在使用的代码。

func updateHealthBars() {
    hullHealthBar.setProgress(hullHealth/hullHealthMax, animated: true)
    cannonsHealthBar.setProgress(cannonsHealth/cannonsHealthMax, animated: true)
    sailsHealthBar.setProgress(sailsHealth/sailsHealthMax, animated: true)
    crewHealthBar.setProgress(crewHealth/crewHealthMax, animated: true)

}

@IBAction func setSailButton(sender: AnyObject) {
    if hullHealth > 0 {
        hullHealth-=20
    }
    else if cannonsHealth > 0 {
        cannonsHealth-=20
    }
    else if sailsHealth > 0 {
        sailsHealth-=20
    }
    else if crewHealth > 0 {
        crewHealth-=20
    }
    updateHealthBars()
}

如果有人知道当我按下按钮时我需要做什么来更新他们的健康状况,我将非常感激,因为我已经尝试了一段时间但没有成功。我正在使用 XCode 6。

最佳答案

在 Swift 中,当使用不带小数的文字分配数字时,它使用 Int。 ProgressView 需要 float 。尝试以下操作:

func updateHealthBars() {
    dispatch_async(dispatch_get_main_queue(), ^{
        hullHealthBar.setProgress(hullHealth/hullHealthMax, animated: true)
        cannonsHealthBar.setProgress(cannonsHealth/cannonsHealthMax, animated: true)
        sailsHealthBar.setProgress(sailsHealth/sailsHealthMax, animated: true)
        crewHealthBar.setProgress(crewHealth/crewHealthMax, animated: true)
    });   
}

@IBAction func setSailButton(sender: AnyObject) {
    if hullHealth > 0.0 {
        hullHealth-=20.0
    }
    else if cannonsHealth > 0.0 {
        cannonsHealth-=20.0
    }
    else if sailsHealth > 0.0 {
        sailsHealth-=20
    }
    else if crewHealth > 0.0 {
        crewHealth-=20.0
    }
    updateHealthBars()
}

确保您的属性也是 float 的: var hullHealth: Float = 0var hullHealth = 0.0

关于ios - UIProgressView 作为健康栏并在按下按钮时更新。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025381/

相关文章:

ios - 如何确定 UITableView 性能低下的原因

ios - 将 UIProgressView 添加到 NSURLConnection?

ios - 下载文件时 UIProgressview 不改变

c# - 应用程序崩溃,NSInvalidLayoutConstraintException,iOS Storyboard设计器ViewController中的红色感叹号

ios - Objective-C 错误 [__NSCFNumber 长度] : unrecognized selector sent to instance

ios - 使用 swift 集成 stripe SDK

ios - 可以将传入的电话号码存储为 iO 上的变量吗?

java - 在 Swift 3 中实现 Java String 的 hashCode() 方法

ios - 知道 UIProgressView 何时停止动画

ios - 将文本放在尽可能大的圆圈中