gpt4 book ai didi

swift - 如何检查来自不同函数的两个变量是否相等?

转载 作者:行者123 更新时间:2023-11-28 12:20:32 26 4
gpt4 key购买 nike

我是 Xcode 和 Swift 的新手,但我正在尽力 :)我需要一点帮助。到目前为止我已经写了这段代码......但我有一个问题,我不知道如何解决......好吧,我的问题是我无法检查两个变量是否相等。更准确地说,我想检查“fileAudio”编号是否等于应用中显示的照片编号。

我还需要一些帮助来显示两张随机图片(leftImage 和 rightImage)和链接到其中一张的“playSound”按钮。此时...我只能将其链接到leftImage。

var soundFiles: [String] = [
"s0",
"s1",
"s2",
"s3",
"s4",
"s5",
"s6",
"s7",
"s8",
"s9"
]

var images1: [UIImage] = [

UIImage(named: "foto0.png")!,
UIImage(named: "foto1.png")!,
UIImage(named: "foto2.png")!,
UIImage(named: "foto3.png")!,
UIImage(named: "foto4.png")!,
UIImage(named: "foto5.png")!,
UIImage(named: "foto6.png")!,
UIImage(named: "foto7.png")!,
UIImage(named: "foto8.png")!
]

var images2: [UIImage] = [
UIImage(named: "foto0.png")!,
UIImage(named: "foto1.png")!,
UIImage(named: "foto2.png")!,
UIImage(named: "foto3.png")!,
UIImage(named: "foto4.png")!,
UIImage(named: "foto5.png")!,
UIImage(named: "foto6.png")!,
UIImage(named: "foto7.png")!,
UIImage(named: "foto8.png")!
]

var happySad: [UIImage] = [
UIImage(named: "sad.png")!,
UIImage(named: "happy.png")!
]

var currentImageIndex = 0
var currentImage2Index = 0


var player: AVAudioPlayer!
var ran1:Int = 0
var ran2:Int = 0


@IBOutlet weak var leftImage: UIImageView!

@IBOutlet weak var rightImage: UIImageView!

@IBOutlet weak var sh: UIImageView!


@IBAction func playSound(_ sender: Any) {

let fileaudio:String = "s"+String(ran1)
let soundFilePath = Bundle.main.url(forResource: fileaudio, withExtension: ".m4a")!
player = try! AVAudioPlayer(contentsOf: soundFilePath)
player.prepareToPlay()
player.play()
}
@IBAction func nextImages(_ sender: Any) {
ran1 = Int(arc4random_uniform(7) + 1)
ran2 = Int(arc4random_uniform(6) + 2)

let imageName1:String = "foto"+String(ran1)
leftImage.image = UIImage.init(named: imageName1)

let imageName2:String = "foto"+String(ran2)
rightImage.image = UIImage.init(named: imageName2)

leftImage.isUserInteractionEnabled = true
self.view.addSubview(leftImage)
let gesture1 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap1))
leftImage.addGestureRecognizer(gesture1)

sh.image = UIImage(named: "question")
rightImage.isUserInteractionEnabled = true
self.view.addSubview(rightImage)
let gesture2 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap2))
rightImage.addGestureRecognizer(gesture2)
}

func singleTap1() {

if currentImageIndex == ran1 {
print("\(currentImageIndex)")
sh.image = UIImage(named: "happy.png")
} else {
print("\(currentImageIndex)")
sh.image = UIImage(named: "sad.png")
}
}

func singleTap2() {
if ran1 == ran2 {
sh.image = UIImage(named: "happy.png")
} else {
sh.image = UIImage(named: "sad.png")
}

}

谢谢你,拉杜

最佳答案

您应该生成一个从 0 到 1 的随机数来代表这两个按钮。一个 singleTap: 函数应该可以处理这两种情况。

fileprivate var leftRightNum: Int!

@IBAction func nextImages(_ sender: Any) {
ran1 = Int(arc4random_uniform(7) + 1)
ran2 = Int(arc4random_uniform(6) + 2)

let imageName1:String = "foto"+String(ran1)
leftImage.image = UIImage.init(named: imageName1)

let imageName2:String = "foto"+String(ran2)
rightImage.image = UIImage.init(named: imageName2)

leftImage.isUserInteractionEnabled = true
self.view.addSubview(leftImage)
let gesture1 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap(_:)))
leftImage.addGestureRecognizer(gesture1)

sh.image = UIImage(named: "question")
rightImage.isUserInteractionEnabled = true
self.view.addSubview(rightImage)
let gesture2 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap(_:)))
rightImage.addGestureRecognizer(gesture2)

leftRightNum = Int(arc4random_uniform(2))
}

@IBAction func playSound(_ sender: Any) {
let fileaudio:String = "s"+String(leftRightNum == 0 ? ran1 : ran2)
let soundFilePath = Bundle.main.url(forResource: fileaudio, withExtension: ".m4a")!
player = try! AVAudioPlayer(contentsOf: soundFilePath)
player.prepareToPlay()
player.play()
}

func singleTap(_ recognizer: UIGestureRecognizer) {
if (recognizer.view == leftImage && leftRightNum == 0) || (recognizer.view == rightImage && leftRightNum == 1) {
sh.image = UIImage(named: "happy.png")
} else {
sh.image = UIImage(named: "sad.png")
}
}

关于swift - 如何检查来自不同函数的两个变量是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44957910/

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