gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:17:38 24 4
gpt4 key购买 nike

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

  1. 我想让它以这种方式工作:

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

  2. 当我打开应用程序时,它只显示两个底部按钮..但没有任何图像。我怎样才能让它显示第一张图片?

  3. 我也喜欢让它有点随机思考......当我点击“nextImage”按钮时,我想显示不同的图像但只有一个与声音相关......所以当我播放时声音..必须只检查与声音匹配的照片。

  4. 此刻,阵列中只有 8 张照片/声音,但是当我在 nextImage 上单击超过 9 次时..图像会不断出现..但声音会从头开始,而且它是不再链接。例如,在第 10 幅图像中..它的 playSound 说它在 1。我怎样才能让它跟随图像索引?

  5. 如何将图片索引转成文字?例如,如果它向我显示图像“foto1”;我想在图像下方显示标签中的文本。

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()

}
}

最佳答案

我认为你把问题复杂化了。

我会亲自创建一个结构,其中包含游戏的每个“关卡”。

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)

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

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

关于ios - 如何将图像随机链接到声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44716097/

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