gpt4 book ai didi

swift - 如何解决这个双击问题?

转载 作者:行者123 更新时间:2023-11-30 12:08:39 24 4
gpt4 key购买 nike

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

当我点击错误答案时,应用程序不执行任何操作。当我选择正确答案时,我必须点击两次。

我该如何解决这个问题?

    //
// 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 次,应用程序也不会执行任何操作。

如果我在应用程序不执行任何操作后按下正确答案。如果我再次按下它,它会说没问题,我可以继续。这种情况仅发生在第一个问题时。之后一切正常。

最佳答案

看来您向 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()

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

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

关于swift - 如何解决这个双击问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46359795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com