如何通过达到特定分数来设定动作?例如弹出窗口或动画。

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.


    }
}

最佳答案

您可以为didSet变量实现taps,每次设置taps的值时都会触发该变量,然后检查设置的值。
这样地:

var taps = 0 {
   didSet {
      if taps == 5 {
          print("You have reaches 5 taps !!")
      }
   }
}

关于swift - 通过得分来激事件作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39467756/

10-11 14:54