2017-07-23 5 views
0

Ich entwickle eine iOS-Quiz-App und jedes Mal, wenn eine Frage gestellt wird, möchte ich einen Countdown-Timer von 15 Sekunden starten. Wenn der Benutzer die Frage nicht beantwortet, wenn der Zähler auf Null steht, wird die Antwort als falsch angesehen. Meine Frage ist, wo würde ich den Countdown-Timer-Code setzen, um sicherzustellen, dass jedes Mal, wenn eine Frage gestellt wird, es dem Benutzer 15 Sekunden gibt, um zu antworten? Ich möchte auch, dass diese Zeit jedes Mal zurückgesetzt wird, wenn eine neue Frage gestellt wird.Wo wird der Countdown-Timer-Code gespeichert?

Mein Code:

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() 
    } 

Antwort

0

Dies sollten Sie einige grundlegende Bausteine ​​geben Sie Ihre Lösung zu erstellen.

// 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 

} 
Verwandte Themen