ios - 暂时禁用点击手势

标签 ios swift

我正在开发一个用于教育用途的测验应用程序,在测试它是否“万无一失”时,我注意到第二次点击会转移到下一个问题,导致我的测验跳过 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 方法中的所有条件移动 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/

相关文章:

swift - 在 Swift 中使用 NSDateComponents 从出生日期计算年龄

ios - WKWebView 在本地网页加载时挂起,具有特定的 Web 配置。

iphone - NSDictionary中的错误

ios - 检查 3D 数组中是否存在元素 Swift

ios - Coredata managedObject 问题 swift

ios - swift 火力地堡 : Update specific objects resulting from Firebase query

ios - UIView 的初始化编码器在调用 instantiateWithOwner 后重新加载

ios - 如何在 iOS 中强制 UISearchBar 文本为大写

ios - Swift 和 Sprite 套件接触不正确

swift - 在 Xcode 中哪里可以找到带有 Swift 语法的框架头文件