ios - 倒数计时器代码放置在哪里?

标签 ios swift timer countdown

我正在开发一个 iOS 测验应用程序,每次提出问题时我都希望启动 15 秒的倒计时器。如果在计数器归零时用户没有回答问题,则认为答案是错误的。我的问题是,我应该在哪里放置倒计时器代码,以确保每次提出问题时都给用户 15 秒的时间来回答?我还希望每次提出新问题时都重置这个时间。

我的代码:

class ViewController: UIViewController {
    //countdown timer
    @IBOutlet weak var questionTimer: UILabel!

    func randomQuestion() {
        //random question
        if questionList.isEmpty {
            questionList = Array(QADictionary.keys)
        }

        let rand = Int(arc4random_uniform(UInt32(questionList.count)))
        questionLabel.text = questionList[rand]

        //matching answer values to go with question keys
        var choices = QADictionary[questionList[rand]]!

        questionList.remove(at: rand)

        //create button
        var button:UIButton = UIButton()

        //variables
        var x = 1
        rightAnswerBox = arc4random_uniform(4)+1

        for index in 1...4
        {
            button = view.viewWithTag(index) as! UIButton

            if (index == Int(rightAnswerBox))
            {
                button.setTitle(choices[0], for: .normal)
            }
            else {
                button.setTitle(choices[x], for: .normal)
                x += 1
            }

            randomImage()

        }
    }

    let QADictionary = ["Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"], "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"], "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]]

    //wrong view segue
    func wrongSeg() {
       performSegue(withIdentifier: "wrongViewSegue", sender: self)
    }

    //proceed screen
    func rightSeg() {
        performSegue(withIdentifier: "rightSeg", sender: self)
    }

    //variables
    var rightAnswerBox:UInt32 = 0
    var index = 0

    //Question Label
    @IBOutlet weak var questionLabel: UILabel!

    //Answer Button
    @IBAction func buttonAction(_ sender: AnyObject) {
        if (sender.tag == Int(rightAnswerBox))
        {
            print ("Correct!")
        }
        else if (sender.tag != Int(rightAnswerBox)) {
            wrongSeg()
            print ("Wrong!")
            questionList = []
        }

        randomQuestion()
    }

    override func viewDidAppear(_ animated: Bool)
    {
        randomQuestion()
    }

    //variables
    var seconds = 15
    var timer = Timer()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

最佳答案

这应该为您提供一些基本构建 block 来构建您的解决方案。

// Timer property
var timer: Timer?


// Call this when a new question is loaded
func startTimer() {

    self.timer?.invalidate()

    self.timer = Timer.scheduledTimer(withTimeInterval: 15.0,
                                      repeats: false,
                                      block: { timer in

                                        // Put whatever code should happen if the user does not answer the question in time. 

    })
    RunLoop.main.add(timer,
                     forMode: .commonModes)

}

// Call this when a question has been answered
func questionAnswered() {

    self.timer?.invalidate()

    // Put whatever should happen if a question was answered in time here

}

关于ios - 倒数计时器代码放置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45269817/

相关文章:

ios - 仅 iPhone 6 模拟器像素颜色错误

ios - 在两个依赖框架中链接静态库时重新定义模块

ios - 图表库中的编译时错误

c# - System.Timers.Timer 生命周期

ios - 修改 MPMoviePlayerViewController 上允许的方向(从 UIWebView 调用)。 iOS 5 和 iOS 6

ios - Storyboard已编译但 iOS 找不到它并崩溃

ios - 重复的对象被追加到数组 swift firebase 中

swift - 具有通用委托(delegate)的通用类。如何区分哪个对象的委托(delegate)正在执行回调?

timer - 调用中的额外参数 'selector' - NSTimer scheduledTimerWithTimeInterval

JavaScript 定时器性能