我正在尝试为集合视图中的每个图像播放声音文件,因此如果用户点击图像,用户将听到该图像的名称。例如,如果用户点击一只猫的图像,用户将听到声音文件“猫”。我创建了图像的集合视图,并将 UITapGestureRecognizer 添加到单元格中。声音文件在代码最底部的 imageTapped 函数中播放,但我不知道如何编码。我不知道之后要编码什么:如果 tap.state == .recognized,让 imageView = tap.view as?UIImageView { 该函数中的当前代码构建,但是当我点击每个图像时我没有听到声音。我真的很感激任何帮助。
class FlashcardsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var animals = ["alligator", "bat", "bear", "beaver", "bird", "butterfly", "camel", "cat", "cheetah", "chicken"]
var bodyparts = ["arm", "ear", "eyes", "foot", "hair", "hand", "head", "leg", "mouth", "nose"]
var classroom = ["backpack", "blackboard", "book", "bookshelf", "calculator", "chair", "chalk", "chalkboard", "clock", "colored pencil"]
var clothes = ["belt", "boots", "coat", "dress", "hat", "jeans", "mittens", "pajamas", "pants", "scarf"]
var family = ["baby", "brother", "dad", "family", "grandma", "grandpa", "grandparents", "mom", "parents", "sister"]
var feelings = ["angry", "cold", "confused", "happy", "hot", "hurt", "sad", "scared", "sick", "tired"]
var food = ["apple", "bacon", "banana", "bean", "blackberry", "blueberry", "broccoli", "butter", "carrot", "cereal"]
var house = ["bathtub", "bed", "bowl", "couch", "cup", "dresser", "fridge", "lamp", "mirror", "plant"]
var transportation = ["ambulance", "bike", "boat", "bus", "car", "firetruck", "helicopter", "motorcycle", "police car", "plane"]
var verbs = ["brush your teeth", "carry", "clap", "color", "cook", "crawl", "cut", "dance", "draw", "drink"]
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var segmentedControl: UISegmentedControl!
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(tap:)))
view.addGestureRecognizer(tap)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]
let sectionArray = arrays[segmentedControl.selectedSegmentIndex]
return sectionArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
let arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]
cell.imageView?.image = UIImage(named: arrays[segmentedControl.selectedSegmentIndex][indexPath.row])
cell.isUserInteractionEnabled = true
return cell
}
@IBAction func segmentedControlAction(_ sender: Any) {
collectionView.reloadData()
}
func playSound(file: String?) {
if let file = file {
let musicFile = URL(fileURLWithPath: file)
do{
try audioPlayer = AVAudioPlayer(contentsOf: musicFile)
audioPlayer.play()
}catch{
}
}
}
@objc func imageTapped(tap: UITapGestureRecognizer) {
_ = tap.view as? UIImageView
print("imageTapped")
let arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]
let sectionArray = arrays[segmentedControl.selectedSegmentIndex]
if tap.state == .recognized, let imageView = tap.view as? UIImageView {
for (image) in sectionArray {
imageView.image = UIImage (named: image)
if let sound = Bundle.main.path(forResource: image, ofType: "wav") {
playSound(file: sound)
}
}
}
}
}