我正在开发一个用于教育用途的测试应用程序,在测试它是否“万无一失”时,我注意到第二次点击将转移到下一个问题,导致我的测试跳过两个问题。
在测验中,我有一个弹出窗口,要么告诉学生他们是正确的,要么告诉他们正确的答案是什么。我将下一个问题的加载延迟了大约4秒,以便让学生再次快速查看问题和可能的答案。
我尝试使用isUserInteractionEnabled=false来防止检测到第二次点击,但似乎没有任何效果。本节代码为:
@IBAction func answerPressed(_ sender: UIButton) {
if sender.tag == selectedAnswer {
self.view.isUserInteractionEnabled = false
ProgressHUD.showSuccess("Correct")
print("correct")
score = score + 1
scoreLabel.text = "Score: \(score)"
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
// Put your code which should be executed with a delay here
self.progressClick = self.progressClick + 1
self.questionNumber = self.questionNumber + 1
self.updateProgress()
self.updateQuestion()
})
self.view.isUserInteractionEnabled = true
}
else {
self.view.isUserInteractionEnabled = false
ProgressHUD.showError("Good Try. \(allQuestions.list[questionNumber].revealAnswer)")
print("wrong")
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), execute: {
// Put your code which should be executed with a delay here
self.progressClick = self.progressClick + 1
self.questionNumber = self.questionNumber + 1
self.updateProgress()
self.updateQuestion()
})
self.view.isUserInteractionEnabled = true
}
}
func updateQuestion(){
if questionNumber < (presentNumber + 10) {
questionDiagram.image = UIImage(named:(allQuestions.list[questionNumber].questionPicture))
questionText.text = "Q \(questionNumber + 1). " + allQuestions.list[questionNumber].question
optionA.setTitle(allQuestions.list[questionNumber].optionA, for: UIControl.State.normal)
optionB.setTitle(allQuestions.list[questionNumber].optionB, for: UIControl.State.normal)
optionC.setTitle(allQuestions.list[questionNumber].optionC, for: UIControl.State.normal)
optionD.setTitle(allQuestions.list[questionNumber].optionD, for: UIControl.State.normal)
selectedAnswer = allQuestions.list[questionNumber].correctAnswer
}
else if questionNumber == allQuestions.list.count {
let alert = UIAlertController(title: "Awesome", message: "Finished all the Quizes. Do you want to start again?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: {action in self.restartQuiz()})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
else if questionNumber == 10 {
let alert = UIAlertController(title: "Well Done", message: "That Quiz is done. The next Quiz will now load.", preferredStyle: .alert)
let restartAction2 = UIAlertAction(title: "Continue", style: .default, handler: {action in self.restartQuiz()})
alert.addAction(restartAction2)
present(alert, animated: true, completion: nil)
}
}
最佳答案
问题:
这是因为您正在启用用户交互。
解决方案:
对于DispatchQueue方法中的所有条件,您需要移动self.view.isUserInteractionEnabled = true
,以便在再次启用它之前等待。
检查此示例:
if sender.tag == selectedAnswer {
self.view.isUserInteractionEnabled = false
ProgressHUD.showSuccess("Correct")
print("correct")
score = score + 1
scoreLabel.text = "Score: \(score)"
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
// Put your code which should be executed with a delay here
self.progressClick = self.progressClick + 1
self.questionNumber = self.questionNumber + 1
self.updateProgress()
self.updateQuestion()
self.view.isUserInteractionEnabled = true //This line moved inside DispatchQueue
})
}
关于ios - 暂时禁用轻按手势,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55053817/