gpt4 book ai didi

ios - 如何为每个collectionView单元格通过摇动手势播放声音?

转载 作者:行者123 更新时间:2023-11-30 12:51:21 24 4
gpt4 key购买 nike

我创建了一个collectionView,它是水平滚动的。我想要的是,例如,当我在第一个单元格上并摇动我的设备时,我想播放阵列中的第一个声音。当我在第二个文件上并摇动我的设备时,我想在数组中播放第二个文件。

我已经走到这一步了:

var selectedIndex : Int = 0

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath.row
}

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?)
{

// var currentRow = self.collectionView.indexPath(for: self.collectionView.visibleCells[0])?.row

if selectedIndex == 0 {
do
{
audioPlayer1 = try AVAudioPlayer(contentsOf:Sound1!, fileTypeHint:nil)
}
catch
{
return print("file not found")
}
audioPlayer1.numberOfLoops = 0
audioPlayer1.prepareToPlay()
audioPlayer1.play()
} else if selectedIndex == 1 {
do
{
audioPlayer2 = try AVAudioPlayer(contentsOf:Sound2!, fileTypeHint:nil)
}
catch
{
return print("file not found")
}
audioPlayer2.numberOfLoops = 0
audioPlayer2.prepareToPlay()
audioPlayer2.play()

}

最佳答案

创建一个 var selectedIndex : Int

override func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!)
{
selectedIndex = indexPath.row
}

现在您可以使用 selectedIndex 访问您的数组并播放所选单元格的声音

关于ios - 如何为每个collectionView单元格通过摇动手势播放声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40911364/

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