2017-07-06 60 views
0

我是新來的Xcode和斯威夫特,但我想我最好:) 我需要一點幫助。我已經寫了這個代碼到目前爲止...但我有一個問題,我不知道如何解決它... 嗯,我的問題是,我無法檢查兩個變量是否相等。更確切地說,我想檢查「fileAudio」號碼是否與應用中顯示的照片號碼相同。如何檢查來自不同函數的兩個變量是否相等?

我還需要一些幫助來顯示兩個隨機圖片(leftImage和rightImage)和「playSound」按鈕以鏈接到它們中的一個。此時......我只能將它與左圖像鏈接起來。

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

} 

謝謝你,拉杜

回答

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") 
    } 
} 
+0

我不想在playSound中產生一個隨機數。我有兩個圖像(左和右)和一個按鈕,playSound。我需要將playSound按鈕鏈接到兩個圖像中的一個(不總是相同)。當我點擊playSound按鈕時,我聽到一個音頻,例如狗吠聲。然後使用必須選擇代表聲音的圖像(左或右)。你能幫我做這件事嗎?請:) –

+0

看看我編輯的答案。 – the4kman

+0

非常感謝你!它的功能就像是一種魅力:D 我可以問你,如何在打開應用程序時讓它顯示2張圖像?現在,當我打開應用程序..我只看到playSound按鈕和nextImages按鈕。如果我點擊playSound,因爲我打開應用程序..它崩潰......錯誤是「預計發現零...」再次感謝! –