使用Swift 2,我将如何创建要在集合 View 上使用的音频文件数组?
有4个选项“海洋”,“鸟类”,“自然”,“波浪”,它们显示为单元格。
单击一个单元格时,我需要它来播放声音。你能帮忙吗?
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>
}
希望对您有所帮助。如果没有,请为您的问题添加更多信息,我们将为您提供帮助。