我是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")
}
}
谢谢你,Radu
答案 0 :(得分:2)
您应该生成一个从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")
}
}