如何通过达到特定分数来设置操作?例如弹出窗口或动画。
import UIKit
class ViewController: UIViewController {
@IBOutlet var ScoreLabel: UILabel!
var taps = 0
var highscore = 200
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
if let storedTaps = defaults.objectForKey("key") as? Int {
self.taps = storedTaps
setLabel(storedTaps)
}
}
@IBAction func ScoreButton(sender: UIButton) {
taps += 1
setLabel(taps)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(taps, forKey: "key")
}
func setLabel(taps:Int) {
ScoreLabel.text = "Taps: \(taps)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
您可以为didSet
变量实施taps
,每次设置taps
的值时都会触发该变量,然后检查它设置的值。
像这样:
var taps = 0 {
didSet {
if taps == 5 {
print("You have reaches 5 taps !!")
}
}
}