[英]Temporarily disable tap gesture
我正在开发一个用于教育的测验应用程序,同时在测试它是否“万无一失”时,发现第二次点击会转移到下一个问题,导致我的测验跳了2个问题。
在测验中,我弹出一个窗口,告诉学生他们是正确的,或者告诉他们正确的答案是什么。 我将下一个问题的加载时间延迟了大约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.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
至
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
})
依此类推。
你可以这样做:
UIApplication.shared.beginIgnoringInteractionEvents()
并传播到下一个测验之后
UIApplication.shared.endIgnoringInteractionEvents()
使用情节提要的一个修正方法是使用清晰的UIView覆盖所有内容:该名称(例如Display),然后在需要时运行以下功能。
func disallowTouch() {
self.DisplayView.isHidden = false
self.DisplayView.isUserInteractionEnabled = false
{
当然,您必须创建另一个功能相反的函数:
func allowTouch() {
self.DisplayView.isHidden = true
self.DisplayView.isUserInteractionEnabled = true
{
我希望这会有所帮助,因为它不是常规解决方案。
问题:
这是因为您正在立即启用用户交互。
解:
您需要针对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
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.