Ios SWIFT 3:将不同的声音文件阵列匹配到CollectionView中的每个部分
使用Swift 3 我已经设置了一个包含7个部分的collectionView——当用户点击一个单元格(按钮)时,一个特定的声音将从一系列声音中播放 那么,如何在collectionView中分割各个部分以匹配特定的声音数组呢 比如说 1) 当用户点击collectionView第1部分中的单元格(按钮)时,soundArray1中的声音将播放 2) 当用户点击collectionView第2部分中的单元格(按钮)时,soundArray2中的声音将播放 3) 当用户点击collectionView第3部分中的单元格(按钮)时,soundArray3中的声音将播放 一直到第7节 这是用户点击cellButton时的当前代码Ios SWIFT 3:将不同的声音文件阵列匹配到CollectionView中的每个部分,ios,arrays,swift,audio,Ios,Arrays,Swift,Audio,使用Swift 3 我已经设置了一个包含7个部分的collectionView——当用户点击一个单元格(按钮)时,一个特定的声音将从一系列声音中播放 那么,如何在collectionView中分割各个部分以匹配特定的声音数组呢 比如说 1) 当用户点击collectionView第1部分中的单元格(按钮)时,soundArray1中的声音将播放 2) 当用户点击collectionView第2部分中的单元格(按钮)时,soundArray2中的声音将播放 3) 当用户点击collectionVi
func numberOfSections(in collectionView: UICollectionView) -> Int {
return sections.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return self.appleProducts.count
} else if section == 1 {
return self.animals.count
} else if section == 2 {
return self.food.count
} else if section == 3 {
return self.activity.count
} else if section == 4 {
return self.travel.count
} else if section == 5 {
return self.objects.count
} else if section == 6 {
return self.symbols.count
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if indexPath.section == 0 {
cell.cellButton.setTitle(appleProducts[indexPath.row], for: UIControlState.normal)
} else if indexPath.section == 1 {
cell.cellButton.setTitle(animals[indexPath.row], for: UIControlState.normal)
} else if indexPath.section == 2 {
cell.cellButton.setTitle(food[indexPath.row], for: UIControlState.normal)
} else if indexPath.section == 3 {
cell.cellButton.setTitle(activity[indexPath.row], for: UIControlState.normal)
} else if indexPath.section == 4 {
cell.cellButton.setTitle(travel[indexPath.row], for: UIControlState.normal)
} else if indexPath.section == 5 {
cell.cellButton.setTitle(objects[indexPath.row], for: UIControlState.normal)
} else if indexPath.section == 6 {
cell.cellButton.setTitle(symbols[indexPath.row], for: UIControlState.normal)
}
cell.cellButton.tag = indexPath.row
return cell
}
@IBAction func cellButton(_ sender: AnyObject) {
let sound = (sender as! UIButton).tag
self.setupAudioPlayer(file: Sounds[sound] as NSString, type: ".m4a")
self.soundPlayer.play()
}
其中声音是声音文件的数组1。但是,此声音文件阵列现在正在collectionView的所有部分播放。我不知道如何分割每个声音数组以匹配collectionView中的每个部分您可以使用
collectionView
方法indexPathForItem(at point:CGPoint)
获得正确的索引路径。大概是这样的:
func didPressBtn(sender: UIButton) {
let point = sender.center
let cvPoint = self.collectionView!.convert(point, from: sender.superview)
let ip = self.collectionView!.indexPathForItem(at: cvPoint)!
print("Button \(ip.section):\(ip.row) pressed")
// You can then use ip.section to decide which array to use
// and ip.row to choose the correct element from the array. Like this:
if (ip.section == 0) {
self.setupAudioPlayer(file: soundsArray1[ip.row] as NSString, type: ".m4a")
self.soundPlayer.play()}
} else if (ip.section == 1) {
self.setupAudioPlayer(file: soundsArray2[ip.row] as NSString, type: ".m4a")
self.soundPlayer.play()}
} else if (ip.section == 2) {
self.setupAudioPlayer(file: soundsArray3[ip.row] as NSString, type: ".m4a")
self.soundPlayer.play()}
} // etc etc
}
关于“对成员的引用不明确”错误,我在上面的代码中假设您使用的是UICollectionViewController
,它具有collectionView
属性,或者在UIViewController
中创建了collectionView
属性来引用集合视图。要添加一个(或选中它),您应该
@IBOutlet var collectionView : UICollectionView!
在您的类定义中。然后在情节提要中,需要将相关场景中的集合视图链接到视图控制器。要执行此操作,请按ctrl键从场景顶部的视图控制器图标(黄色圆圈)拖动到集合视图。出现提示时,从插座列表中选择“collectionView”:
您可以在右侧窗格的连接检查器中检查链接是否已建立:
您可以使用
collectionView
方法indexPathForItem(at point:CGPoint)
获取正确的索引路径。大概是这样的:
func didPressBtn(sender: UIButton) {
let point = sender.center
let cvPoint = self.collectionView!.convert(point, from: sender.superview)
let ip = self.collectionView!.indexPathForItem(at: cvPoint)!
print("Button \(ip.section):\(ip.row) pressed")
// You can then use ip.section to decide which array to use
// and ip.row to choose the correct element from the array. Like this:
if (ip.section == 0) {
self.setupAudioPlayer(file: soundsArray1[ip.row] as NSString, type: ".m4a")
self.soundPlayer.play()}
} else if (ip.section == 1) {
self.setupAudioPlayer(file: soundsArray2[ip.row] as NSString, type: ".m4a")
self.soundPlayer.play()}
} else if (ip.section == 2) {
self.setupAudioPlayer(file: soundsArray3[ip.row] as NSString, type: ".m4a")
self.soundPlayer.play()}
} // etc etc
}
关于“对成员的引用不明确”错误,我在上面的代码中假设您使用的是UICollectionViewController
,它具有collectionView
属性,或者在UIViewController
中创建了collectionView
属性来引用集合视图。要添加一个(或选中它),您应该
@IBOutlet var collectionView : UICollectionView!
在您的类定义中。然后在情节提要中,需要将相关场景中的集合视图链接到视图控制器。要执行此操作,请按ctrl键从场景顶部的视图控制器图标(黄色圆圈)拖动到集合视图。出现提示时,从插座列表中选择“collectionView”:
您可以在右侧窗格的连接检查器中检查链接是否已建立:
使用
indexPathForItem(在点:CGPoint)->IndexPath?
(使用按钮的中心作为点),然后使用indexPathForItem.section
确定要从哪个数组中选择(使用IndexPath.row
)。indexPathForItem似乎没有函数?最近的是indexPathSee上的DidSelectItem。谢谢-你能再详细一点吗-例如indexpathforitem是一个独立的函数还是在我的手机按钮操作代码中?我的意思是将这些函数放在哪里最合适?使用indexPathForItem(在点:CGPoint)->IndexPath?
(使用按钮的中心作为点),然后使用IndexPath.section
确定要从中选择的数组(使用IndexPath.row
).ExpathForItem中似乎没有函数?最近的是indexPathSee上的DidSelectItem。谢谢-你能再详细一点吗-例如indexpathforitem是一个独立的函数还是在我的手机按钮操作代码中?我的意思是把这些函数放在哪里最好?func indexPathForItem(在点:CGPoint)->indexath?{if IndexPath.section==0{return Sounds[IndexPath.row]}否则if IndexPath.section==1{}返回nil}这是我到目前为止设置的功能,但是有很多错误-对swift来说非常新,所以越详细越好-谢谢@GuydeBromhead无需实现该函数(indexPathForItem(at:)
):collectionView已经有了该方法。就像我在上面的代码中一样多地使用它。我将对其进行扩展,以精确显示如何获得正确的声音播放效果。非常感谢-我得到了基于此代码的最后一个错误-在这个linefunc indexPathForItem(在点:CGPoint)->IndexPath?{if IndexPath.section==0{return Sounds[IndexPath.row]}如果IndexPath.section==1{}return nil}这是我到目前为止设置的函数,但有很多错误-对swift来说非常新,所以越详细越好-谢谢!!@GuydeBromhead无需实现该函数(indexPathForItem(at:)
):collectionView已经有了这个方法。只需像我在上面代码中那样使用它。我将扩展它以精确显示如何获得正确的声音播放。非常感谢-我在这一行收到了基于此代码的最后一个错误