如何使用 Swift 2 创建要在 Collection View 上使用的音频文件数组?
有 4 个选项“Ocean”、“Birds”、“Nature”、“Waves”,它们显示为单元格。
单击单元格时,我需要它来播放声音。你能帮忙吗?
var imageArray = [UIImage(named: "ocean"),UIImage(named: "birds"),UIImage(named: "nature"),UIImage(named: "wave")]
var nameArray = ["Ocean","Birds","Nature","Waves"]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! homeCollectionViewCell
cell.iconImage?.image = imageArray[indexPath.item!]
cell.label.text = nameArray[indexPath.item!]
return cell
}
最佳答案
你知道怎么播放声音吗?你做到了吗? 如果是这样,您需要使用以下函数并执行如下操作:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//grab the sound from the array
let sound = your_array_with_sounds[indexPath.row]
//if you want to play the sound in this view:
<code to play the sound>
//if you want to play the sound in another view:
<perform the segue and pass the sound to the view you want>
}
希望对您有所帮助。如果没有,请在您的问题中添加更多信息,我们将为您提供帮助。
关于arrays - Swift:音频文件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135258/