如何解决这个双击问题?

时间:2017-09-22 08:04:49

标签: swift xcode

在我的应用程序中一切正常,我唯一的问题是,当我加载应用程序时,第一个视图无法正常工作。

当我点击错误的答案时,应用程序什么都不做。当我选择正确的答案时,我必须点击两次。

我该如何解决这个问题?

    //
    //  EViewController.swift
    //  imageMeaning
    //
    //  Created by RaduVille on 17/08/17.
    //  Copyright © 2017 RaduVille. All rights reserved.
    //

    //
    //  MViewController.swift
    //  imageMeaning
    //
    //  Created by RaduVille on 17/08/17.
    //  Copyright © 2017 RaduVille. All rights reserved.
    //

     import UIKit

    class EViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        currentQuestion = questions[0]
        setQuestion()
        firstOption.isUserInteractionEnabled = true
        secondOption.isUserInteractionEnabled = true
        thirdOption.isUserInteractionEnabled = true
        allowedToGoFurther = 0

        }
        override func viewDidAppear(_ animated: Bool) {

       }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // imageMeaning starts here
    //layout links to the code starts here
    //this IBOutlet represints the message shown on top

    @IBOutlet weak var topMessage: UILabel!
    // these outlets represints the text shown on the buttons

    @IBOutlet weak var firstOption: UIButton!
    @IBOutlet weak var secondOption: UIButton!
    @IBOutlet weak var thirdOption: UIButton!
    //represints the images on the right "✅" or "❌"

    @IBOutlet weak var firstImage: UIImageView!
    @IBOutlet weak var secondImage: UIImageView!
    @IBOutlet weak var thirdImage: UIImageView!

    //this is the big image on the left

    @IBOutlet weak var leftImage: UIImageView!
    @IBOutlet weak var nextB: UIButton!

    @IBAction func nextButton(_ sender: Any) {

                //this is the next button
        if allowedToGoFurther == 0 {
            self.nextB.isEnabled = false
        }
        if(currentQuestionPos + 1 < questions.count) {
            currentQuestionPos += 1
            currentQuestion = questions[currentQuestionPos]
            setQuestion()
            firstImage.image = UIImage(named: "question.png")
            secondImage.image = UIImage(named: "question.png")
            thirdImage.image = UIImage(named: "question.png")
            firstOption.isUserInteractionEnabled = true
            secondOption.isUserInteractionEnabled = true
            thirdOption.isUserInteractionEnabled = true
        } else {
            loadNextQuestion()
            firstImage.image = UIImage(named: "question.png")
            secondImage.image = UIImage(named: "question.png")
            thirdImage.image = UIImage(named: "question.png")
        }
    }

    struct Question {
        let image: UIImage
        let answers: [String]
        let correctAnswer: Int
        let corect: String
    }

    var questions: [Question] = [
        Question(
            image: UIImage(named: "palla")!,
            answers: ["cerchio", "palla", "aereo"],
            correctAnswer: 1,
            corect: "palla"),
        Question(
            image: UIImage(named: "guanto")!,
            answers: ["guanto", "maglietta", "calzino"],
            correctAnswer: 0,
            corect: "guanto"),
        Question(
            image: UIImage(named: "casa")!,
            answers: ["albero", "macchina", "casa"],
            correctAnswer: 2,
            corect: "casa"),
        Question(
            image: UIImage(named: "cerchio")!,
            answers: ["cerchio", "sole", "palla"],
            correctAnswer: 0,
            corect: "cerchio"),
        Question(
            image: UIImage(named: "lego")!,
            answers: ["bambola", "lego", "panino"],
            correctAnswer: 1,
            corect: "lego"),
        Question(
            image: UIImage(named: "chiavi")!,
            answers: ["porta", "pizza", "chiavi"],
            correctAnswer: 2,
            corect: "chiavi"),
        Question(
            image: UIImage(named: "tazza")!,
            answers: ["tazza", "forchetta", "piatto"],
            correctAnswer: 0,
            corect: "tazza"),
        Question(
            image: UIImage(named: "aereo")!,
            answers: ["bicicletta", "gelato", "aereo"],
            correctAnswer: 2,
            corect: "aereo"),
        Question(
            image: UIImage(named: "macchina")!,
            answers: ["televisore", "macchina", "pattini"],
            correctAnswer: 1,
            corect: "macchina"),
        Question(
            image: UIImage(named: "libro")!,
            answers: ["scatola", "foglio", "libro"],
            correctAnswer: 2,
            corect: "libro"),
        Question(
            image: UIImage(named: "piano")!,
            answers: ["piano", "chittara", "arpa"],
            correctAnswer: 0,
            corect: "piano"),
        Question(
            image: UIImage(named: "dadi")!,
            answers: ["dadi", "ghiaccio", "cubo"],
            correctAnswer: 0,
            corect: "dadi"),
        Question(
            image: UIImage(named: "pizza")!,
            answers: ["hamburger", "panino", "pizza"],
            correctAnswer: 2,
            corect: "pizza"),
        Question(
            image: UIImage(named: "palla")!,
            answers: ["cerchio", "palla", "aereo"],
            correctAnswer: 1,
            corect: "palla")
    ]

    var currentQuestion: Question?
    var currentQuestionPos = 0
    var noCorrect = 0
    var Corect = 0
    var allowedToGoFurther = 0




    //
    //
    //

    //here starts the code about the first button action
    @IBAction func firstOption(_ sender: Any) {
        checkAnswer(idx: 0)
            }
    //here starts the code about the second button action
    @IBAction func secondOption(_ sender: Any) {
        checkAnswer(idx: 1)
    }
    //here starts the code about the third button action
    @IBAction func thirdOption(_ sender: Any) {
        checkAnswer(idx: 2)
    }

    func checkAnswer(idx: Int) {
        if(idx == currentQuestion!.correctAnswer) {
            print("corect")
            noCorrect += 1
            let gesture1 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap1(_:)))
            firstOption.addGestureRecognizer(gesture1)
            let gesture2 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap2(_:)))
            secondOption.addGestureRecognizer(gesture2)
            let gesture3 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap3(_:)))
            thirdOption.addGestureRecognizer(gesture3)

        }
    }
    @objc func singleTap1(_ recognizer: UIGestureRecognizer) {
        print("am tastat1")
        if (firstOption.currentTitle != currentQuestion!.corect) {
            print("not true, i go further")
            firstImage.image = UIImage(named: "false.png")
            firstOption.isUserInteractionEnabled = false
            secondOption.isUserInteractionEnabled = false
            thirdOption.isUserInteractionEnabled = false
            allowedToGoFurther = 1
            self.nextB.isEnabled = true

        } else {
            firstImage.image = UIImage(named: "true.png")
            firstOption.isUserInteractionEnabled = false
            secondOption.isUserInteractionEnabled = false
            thirdOption.isUserInteractionEnabled = false
            allowedToGoFurther = 1
            self.nextB.isEnabled = true
        }
    }
    @objc func singleTap2(_ recognizer: UIGestureRecognizer) {
        print("am tastat2")
        if (secondOption.currentTitle != currentQuestion!.corect) {
            print("second button. Not true, I go further")
            secondImage.image = UIImage(named: "false.png")
            secondOption.isUserInteractionEnabled = false
            firstOption.isUserInteractionEnabled = false
            thirdOption.isUserInteractionEnabled = false
            allowedToGoFurther = 1
            self.nextB.isEnabled = true

        } else {
            secondImage.image = UIImage(named: "true.png")
            firstOption.isUserInteractionEnabled = false
            secondOption.isUserInteractionEnabled = false
            thirdOption.isUserInteractionEnabled = false
            allowedToGoFurther = 1
            self.nextB.isEnabled = true
        }
    }
    @objc func singleTap3(_ recognizer: UIGestureRecognizer) {
        print("am tastat3")
        if (thirdOption.currentTitle != currentQuestion!.corect) {
            print("third button. Not true, I go further")
            thirdImage.image = UIImage(named: "false.png")
            thirdOption.isUserInteractionEnabled = false
            firstOption.isUserInteractionEnabled = false
            secondOption.isUserInteractionEnabled = false
            allowedToGoFurther = 1
            self.nextB.isEnabled = true

        } else {
            thirdImage.image = UIImage(named: "true.png")
            firstOption.isUserInteractionEnabled = false
            secondOption.isUserInteractionEnabled = false
            thirdOption.isUserInteractionEnabled = false
            allowedToGoFurther = 1
            self.nextB.isEnabled = true
        }
    }
    func loadNextQuestion() {
        if(currentQuestionPos + 1 < questions.count){
            currentQuestionPos += 1
            currentQuestion = questions[currentQuestionPos]
            setQuestion()
            firstOption.isUserInteractionEnabled = true
            secondOption.isUserInteractionEnabled = true
            thirdOption.isUserInteractionEnabled = true
            allowedToGoFurther = 0
        } else {
            performSegue(withIdentifier: "easyRestart", sender: nil)
        }
    }
    func setQuestion() {
        leftImage.image = currentQuestion!.image
        firstOption.setTitle(currentQuestion!.answers[0], for: .normal)
        secondOption.setTitle(currentQuestion!.answers[1], for: .normal)
        thirdOption.setTitle(currentQuestion!.answers[2], for: .normal)
        self.nextB.isEnabled = false
    }
    var easy = 0

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "easyRestart") {
            let vc = segue.destination as! startOver
            vc.easy = 1
            vc.noCorrect = 0
            vc.total = questions.count
        }
    }

}

所以,当我点击错误的答案时,即使我按了100次,该应用程序也什么都不做。

如果应用程序什么都不做,我会按正确答案。如果我再次按下它就说它没关系,我可以走得更远。 这只发生在第一个问题上。之后一切正常。

1 个答案:

答案 0 :(得分:0)

这似乎是你在UIButton中添加一个手势识别器,而UIButton的默认手势会取消你要添加的手势,尝试使用:

button.addTarget(self, action: #selector(...), for: .touchUpInside)

在您的代码中

  let gesture1 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap1(_:)))
  firstOption.addGestureRecognizer(gesture1)
  let gesture2 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap2(_:)))
  secondOption.addGestureRecognizer(gesture2)
  let gesture3 = UITapGestureRecognizer(target: self, action: #selector(EViewController.singleTap3(_:)))
  thirdOption.addGestureRecognizer(gesture3)

firstOption.addTarget(self, action: #selector(singleTap1), for: .touchUpInside)
secondOption.addTarget(self, action: #selector(singleTap2), for: .touchUpInside)
thirdOption.addTarget(self, action: #selector(singleTap3), for: .touchUpInside)

并删除参数:

@objc func singleTap1()
@objc func singleTap2()
@objc func singleTap3()

但每次按下按钮都会添加目标。

我真的不知道你想要实现什么