gpt4 book ai didi

swift - 检查多个 bool 值并根据该 bool 值更改图像的最快方法

转载 作者:行者123 更新时间:2023-11-28 08:31:29 25 4
gpt4 key购买 nike

我有多个用户可以在游戏中获得的成就。当用户进入成就VC时,他应该看到成就完成时的图像。

我现在在 viewDidLoad 函数中使用这段代码:

if unlockedAchievement1 == true
{
achievement1Text.textColor = UIColor.greenColor()
achievement1Image.image = UIImage(named: "achievement1Unlocked")
}
if unlockedAchievement2 == true
{
achievement2Text.textColor = UIColor.greenColor()
achievement2Image.image = UIImage(named: "achievement2Unlocked")
}

这样可以,但是复制粘贴这个加类要花很多时间。我怎样才能缩短这个?我应该上课吗?我读过 for in loops,但我不太明白。欢迎任何帮助!

最佳答案

我感觉我以前见过这个。但无论如何...

//use array of bools. First declare as empty array.
//Use Core data to achieve saving the Booleans if you need to.
let achievements:[Bool] = []
let achievementsImage:[UIImage] = []
let achievementsText:[UITextView] = []

func viewDidLoad() {
//Say you have 5 achievements.
for index in 0 ..< 5 {
achievements.append(false)
achievementsImage.append(UIImage(named: "achievementLocked"))
achievementText.append(UITextView())
//Then you can edit it, such as.
achievementText[index].frame = CGRect(0, 0, 100, 100)
achievementText[index].text = "AwesomeSauce"
self.view.addSubview(achievementText[index])
achievementText[index].textColor = UIColor.blackColor()
}
}

//Call this function and it will run through your achievements and determine what needs to be changed
func someFunction() {
for index in 0 ..< achievements.count {
if(achievements[index]) {
achievements[index] = true
achievementsImage[index] = UIImage(named: String(format: "achievement%iUnlocked", index))
achievementsText[index].textColor = UIColor.greenColor
}
}
}

关于swift - 检查多个 bool 值并根据该 bool 值更改图像的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38797978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com