我正在创建拼图文字游戏,即使用户退出应用程序,如何增加,保存和更新分数?但是我希望在UILabel上添加点并在完成动画后增加
代码:
class QuestionView: UIViewController {
var score = 0
func animate(sender: UIButton) {
UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { [weak self] in
sender.transform = .identity
}) { (finished) in
score += 2 // Must be increase by 2
}
}
func createCoins() {
let lblCoins = UILabel()
lblCoins.text = "\(score)"
self.view.addSubview(lblCoins)
}
}
答案 0 :(得分:0)
更新得分变量后,您还必须更新 lblCoins.text 。 要将数据保存在磁盘上,您可以写入设备文档目录中的文件。 保存每次分数变化的数据。
这可以帮到你: http://www.seemuapps.com/read-to-and-write-from-a-text-file-in-swift
应用程序启动从文件读取并设置您的分数变量。 您可以在 viewDidLoad 中执行此操作。
答案 1 :(得分:0)
我认为您正在询问如何跟踪值(分数),以便您可以在用户再次打开应用时检索它。如果是这种情况,那么您可以使用UserDefaults
database。
UserDefaults
非常适合存储少量数据,例如分数或用户偏好。
以下示例中显示的synchronize
方法调用是可选的:iOS会不时自动拨打该电话。但是,如图所示,您可以手动调用它,以确保您的分数在适当的时间更新。
Swift 3.x:
// define a constant String name for the key where the score will be stored
// (note: this can be any name you want; I randomly chose "scoreKey")
let scoreKey = "scoreKey"
// even if the key's value isn't Bool, iOS will return 'false'
// if the key hasn't been set or 'true' if it has been set
let scoreKeyExists: Bool = NSUserDefaults.standardUserDefaults().boolForKey(scoreKey)
// if no value has been set, yet, then set an initial value
if !scoreKeyExists {
NSUserDefaults.standardUserDefaults().setInt(0, forKey: scoreKey)
NSUserDefaults.standardUserDefaults().synchronize()
}
// retrieve the value when needed
let currentScore = NSUserDefaults.standardUserDefaults().intForKey(scoreKey)
// set value as needed
let newScore = 10
NSUserDefaults.standardUserDefaults().setInt(newScore, forKey: scoreKey)
NSUserDefaults.standardUserDefaults().synchronize()
斯威夫特4:
// define a constant String name for the key where the score will be stored
// (note: this can be any name you want; I randomly chose "scoreKey")
let scoreKey = "scoreKey"
// even if the key's value isn't Bool, iOS will return 'false'
// if the key hasn't been set
let scoreKeyExists: Bool = UserDefaults.standard.bool(forKey: scoreKey)
// if no value has been set, yet, then set an initial value
if !scoreKeyExists {
UserDefaults.standard.set(0, forKey: scoreKey)
UserDefaults.standard.synchronize()
}
// retrieve the value when needed
let currentScore = UserDefaults.standard.int(forKey: scoreKey)
// set value as needed
let newScore = 10
UserDefaults.standard.set(newScore, forKey: scoreKey)
UserDefaults.standard.synchronize()
而且,为了在finish
关闭期间更新您的标签,您需要使您的标签成为一个全局变量,就像您的分数一样。
您可以通过Interface Builder将其添加为IBOutlet
。查看本教程,了解如何Connect the UI to Code。
答案 2 :(得分:0)
这是我的解决方案:
func animate(sender: UIButton) {
UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6, options: .allowUserInteraction, animations: { [weak self] in
sender.transform = .identity
}) { (finished) in
score += 2
lblScoreCoins.text = String(score)
UserDefaults.standard.set(lblScoreCoins.text, forKey: "Key")
}
}