暂时禁用轻按手势

时间:2019-03-07 22:27:47

标签: 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)   
    }  
}

4 个答案:

答案 0 :(得分:0)

重新启用用户交互是需要延迟的一部分。更改

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

依次类推。

答案 1 :(得分:0)

您可以执行以下操作:

UIApplication.shared.beginIgnoringInteractionEvents() 

并且传播到下一个测验

UIApplication.shared.endIgnoringInteractionEvents()

答案 2 :(得分:0)

对情节提要的修复是使用清晰的UIView覆盖所有内容:例如显示的名称,然后在需要时运行以下功能。

func disallowTouch() {
    self.DisplayView.isHidden = false
    self.DisplayView.isUserInteractionEnabled = false
{

您当然必须创建另一个相反的功能:

func allowTouch() {
    self.DisplayView.isHidden = true
    self.DisplayView.isUserInteractionEnabled = true
    {

我希望这会有所帮助,因为这不是常规的解决方法。

答案 3 :(得分:0)

问题:

这是因为您正在立即启用用户交互。

解决方案:

您需要将self.view.isUserInteractionEnabled = true移至DispatchQueue方法中的所有条件,以便等待再次启用它。

检查此示例:

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

}