タイマーを使ってゲームを終了させよう

タイマーを使う

var time = 20
var timer:Timer?
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countTime), userInfo: nil, repeats: true)
@objc func countTime(){
    time -= 1
    if time == 0 {
        //時間になった時に実行するコード
    }
}

画面遷移

performSegue(withIdentifier: "result", sender: nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let nextVC = segue.destination as! ResultViewController
    nextVC.point = point
}

完成したコード

import UIKit

class ResultViewController: UIViewController {
    
    var point = 0
    @IBOutlet weak var recordLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        recordLabel.text = "あなたの記録 \(point)P"
    }
    
    @IBAction func onBackTop(_ sender: Any) {
        navigationController?.popToRootViewController(animated: true)
    }
    
}
import UIKit

class CalcViewController: UIViewController {

    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var resultLabel: UILabel!
    
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!
    @IBOutlet weak var button4: UIButton!
    
    var answer = 0
    var point = 0
    var time = 20
    
    var timer:Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countTime), userInfo: nil, repeats: true)
        
        makeNewQuestion()
        setButtonTitle()
        
        button1.addTarget(self, action: #selector(onButton(_:)), for: .touchUpInside)
        button2.addTarget(self, action: #selector(onButton(_:)), for: .touchUpInside)
        button3.addTarget(self, action: #selector(onButton(_:)), for: .touchUpInside)
        button4.addTarget(self, action: #selector(onButton(_:)), for: .touchUpInside)
    }
    
    func makeNewQuestion(){
        let num1 = Int.random(in: 0...9)
        let num2 = Int.random(in: 0...9)
        questionLabel.text = "\(num1) + \(num2) ="
        answer = num1 + num2
    }
    
    func setButtonTitle(){
        var answerList = [answer]
        while answerList.count < 4 {
            let randomAnswer = Int.random(in: 0...18)
            if !answerList.contains(randomAnswer) {
                answerList.append(randomAnswer)
            }
        }
        answerList.shuffle()
        button1.setTitle(String(answerList[0]), for: .normal)
        button2.setTitle(String(answerList[1]), for: .normal)
        button3.setTitle(String(answerList[2]), for: .normal)
        button4.setTitle(String(answerList[3]), for: .normal)
    }
    
    @objc func onButton(_ sender:UIButton){
        let input = Int(sender.currentTitle!)
        if input == answer {
            point += 10
            resultLabel.text = "前回の問題 : 正解"
        } else {
            point -= 10
            resultLabel.text = "前回の問題 : 不正解"
        }
        makeNewQuestion()
        setButtonTitle()
    }
    
    @objc func countTime(){
        time -= 1
        timerLabel.text = "残り\(time)秒"
        if time == 0 {
            timer!.invalidate()
            performSegue(withIdentifier: "result", sender: nil)
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nextVC = segue.destination as! ResultViewController
        nextVC.point = point
    }

}

コメント

タイトルとURLをコピーしました