如何将图像随机链接到声音?

时间:2017-06-23 07:59:16

标签: ios swift xcode random

我有这个代码并且它正在工作,但不是真的想要。我是Xcode / Swift /开发应用程序的新手。我已经看过教程并使用一些片段作为示例,但到目前为止工作:)

  1. 我想以这种方式让它发挥作用:

    当我点击playSound并听到它时,我必须在leftImage和rightImage ...与playSound匹配的图像之间进行选择。我已经尝试了很多方法让它工作..但没有...我不能使声音索引成为一个字符串来比较为“if a == b ..”

  2. 当我打开应用程序时,它只向我显示两个底部按钮..但不是任何图像。如何让它显示第一张图片?

  3. 我也想让它有点随机想想...当我点击“nextImage”按钮时,我想要显示不同的图像,但只有一个链接到声音..所以我玩的时候声音......只能检查与声音匹配的照片。

  4. 此时,我只有8张照片/声音进入阵列,但是当我在nextImage上点击超过9次时...图像正在继续......但声音从头开始,它是没有联系。例如,在第10张图片上... playSound说它在1.如何使它跟随图像索引?

  5. 如何将图像索引转换为文本?例如,如果它显示图像“foto1”;我想在图片下面给我看标签上的文字。

  6. import UIKit
    import AVFoundation
    
    class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    var soundFiles: [String] = [
        "s0",
        "s1",
        "s2",
        "s3",
        "s4",
        "s5",
        "s6",
        "s7",
        "s8",
        "s9"
    ]
    
    var images1: [UIImage] = [
    
        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: "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
    
    
    @IBOutlet weak var leftImage: UIImageView!
    
    @IBOutlet weak var rightImage: UIImageView!
    
    @IBOutlet weak var sh: UIImageView!
    
    
    
    
    
    @IBAction func nextImages(_ sender: Any) {
     currentImageIndex += 1
        let numberOfImages = images1.count
        let nextImage1Index = currentImageIndex % numberOfImages
        leftImage.image = images1[nextImage1Index]
        leftImage.isUserInteractionEnabled = true
        self.view.addSubview(leftImage)
        let gesture1 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap1))
        leftImage.addGestureRecognizer(gesture1)
    
    
     currentImage2Index += 1
        let numberOfImages2 = images2.count
        let nextImage2Index = currentImage2Index % numberOfImages2
        rightImage.image = images2[nextImage2Index]
        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 == currentImage2Index {
            sh.image = UIImage(named: "happy.png")
            print("ok")
        } else {
            sh.image = UIImage(named: "sad.png")
            print("not ok")
        }
    }
    func singleTap2() {
        if currentImageIndex == currentImage2Index {
            sh.image = UIImage(named: "happy.png")
            print("ok2")
        } else {
            sh.image = UIImage(named: "sad.png")
            print("not ok2")
        }
    }
    
    
    var player: AVAudioPlayer!
    
    @IBAction func playSound(_ sender: Any) {
        let numberOfImages = images1.count
        let nextImage5Index = currentImageIndex % numberOfImages
        let soundFilePath = Bundle.main.url(forResource: soundFiles[nextImage5Index], withExtension: ".m4a")!
        player = try! AVAudioPlayer(contentsOf: soundFilePath)
        player.prepareToPlay()
        player.play()
    
    }
    }
    

1 个答案:

答案 0 :(得分:1)

我认为你的问题太复杂了。

我个人会创建一个包含每个级别'游戏。

enum correctImageType {
    case left, right
}

struct Level {
   var word: String
   var leftImage: UIImage
   var rightImage: UIImage
   var soundFile: String
   var correctImage: correctImageType
}

var level1 = Level(word: "Dog", leftImage: UIImage(named: "dog"), rightImage: UIImage(named: "cat", soundFile: "Woof", correctImage: .left)

这为您提供了一个数据结构中足够的信息来显示每个级别。然后,您可以创建这些项目的数组,对它们进行排序,对它们进行随机排序,在每个项目完成时标记为完整等。

您的问题中有很多问题。我会尝试使其更具体,并一次解决问题的一个特定部分。发布多个问题只要它们不同就没有问题。