gpt4 book ai didi

ios - Swift:测验应用程序,计时器 = 0 时的 "Time out!"消息

转载 作者:行者123 更新时间:2023-11-28 14:24:06 24 4
gpt4 key购买 nike

我正在构建一个测验应用程序,用户有 15 秒的时间来回答问题。我是 swift 的新手,所以我不确定如何在 timer.secondsRemaining == 0 时显示超时消息:questionField.text = "超时!"并禁用带有答案的按钮。

我在一个单独的 swift 文件中设置了一个计时器类:

import Foundation

class questionTimer {
var timer = Timer()
var secondsRemaining: Float = 15
var isOn = false

func start() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
isOn = true
print("time started")
}

@objc func updateCounter() {
secondsRemaining -= 1
print(secondsRemaining)
if secondsRemaining == 0 {
timer.invalidate()
print("time over")
}
}

func reset() {
secondsRemaining = 15
timer.invalidate()
isOn = false
print("time stopped")
}

我的 viewController 看起来像这样:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

// MARK: - Properties

let gameManager = GameManager()
let timer = questionTimer()
var buttons = [UIButton]()

// MARK: - Outlets

@IBOutlet weak var header: UIView!
@IBOutlet weak var questionTracker: UILabel!
@IBOutlet weak var questionField: UILabel!
@IBOutlet weak var option1Button: UIButton!
@IBOutlet weak var option2Button: UIButton!
@IBOutlet weak var option3Button: UIButton!
@IBOutlet weak var option4Button: UIButton!
@IBOutlet weak var playAgainButton: UIButton!

// MARK: - Actions

@IBAction func checkANswer(_ sender: UIButton) {
// Disable all buttons
for button in buttons {
button.isEnabled = false
}

// Check the answer selected and display feedback
if gameManager.isCorrect(sender.tag) {
questionField.text = "Correct!"
playCorrectSound()
sender.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
} else {
questionField.text = "Sorry, that's not it."

// Highlight correct answer
let correctAnswer = self.gameManager.quiz.questions[self.gameManager.indexOfSelectedQuestion].correctAnswer
}

// reset timer
timer.reset()

// call next round
loadNextRound(delay: 2)
}

@IBAction func playAgain(_ sender: UIButton) {
// Reset game manager properties to 0
gameManager.correctQuestions = 0
gameManager.questionsAsked = 0

// call next round
nextRound()
}

// MARK: - Helpers

func displayAnswers() {
// Reset display without any button
hideAllButtons()
resetButtonsStyle()
buttons = []

// create buttons depending on number of answers of the selected question
let questionSelected = gameManager.quiz.questions[gameManager.indexOfSelectedQuestion]
let numOfAnswers = questionSelected.possibleAnswers.count

switch numOfAnswers {
case 1: buttons += [option1Button]
case 2: buttons += [option1Button, option2Button]
case 3: buttons += [option1Button, option2Button, option3Button]
case 4: buttons += [option1Button, option2Button, option3Button, option4Button]
default: print("This question has no answers")
}

for button in buttons {
let index = button.tag
button.setTitle(questionSelected.possibleAnswers[index], for: .normal)
button.isHidden = false
button.isEnabled = true
}
}

func displayQuestion() {
// Select random question
let questionSelected = gameManager.getRandomQuestion()

// Set labels text
questionField.text = questionSelected.question
questionTracker.text = "Question \(gameManager.questionsAsked) of \(gameManager.questionsPerRound)"

// Display answers
displayAnswers()

// HIde play button
playAgainButton.isHidden = true

// Start timer
timer.start()
}

func displayScore() {
}

func nextRound() {
resetButtonsStyle()
if gameManager.questionsAsked == gameManager.questionsPerRound {
// Game is over
displayScore()
gameManager.previousQuestionIndexes = []
} else {
// Continue game
displayQuestion()
}
}

func loadNextRound(delay seconds: Int) {
// Converts a delay in seconds to nanoseconds as signed 64 bit integer
let delay = Int64(NSEC_PER_SEC * UInt64(seconds))
// Calculates a time value to execute the method given current time and delay
let dispatchTime = DispatchTime.now() + Double(delay) / Double(NSEC_PER_SEC)

// Executes the nextRound method at the dispatch time on the main queue
DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
self.nextRound()
}
}

override func viewDidLoad() {
super.viewDidLoad()
displayQuestion()
questionTracker.text = "Question \(gameManager.questionsAsked) of \(gameManager.questionsPerRound)"
}

因为 updateCounter() 方法在一个单独的文件中,我怎样才能让它改变 View Controller 中的 questionField.text?

如果我把它放在 viewController 中它不起作用:

// Time out
if timer.secondsRemaining == 0 {
print("time out")
questionField.text = "Time out!"
for button in buttons {
button.isEnabled = false
}
timer.reset()

最佳答案

一个简单的解决方案是观察 secondsRemaining:

var secondsRemaining: Float = 15 {
didSet {
if secondsRemaining == 0 {
// update UI
}
}
}

我猜你也可以把它改成一个Int

关于ios - Swift:测验应用程序,计时器 = 0 时的 "Time out!"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51740230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com