我在ViewController中有一个CollectionView。此collectionView中的每个collectionViewCell都有一个按钮。按钮按下后,音频开始播放。我想要做的是在音频播放结束后隐藏按钮。
如何在audioPlayerDidFinishPlaying
委托方法中访问collectionViewCell中按钮的isHidden属性?
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print("Did finish playing audio.")
}
发布于 2018-04-05 14:24:28
您需要知道要在其中隐藏按钮的单元格的indexPath
。然后,在你的audioPlayerDidFinishPlaying
方法中,只需执行以下操作:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if let cell = collectionView.cellForItem(at: savedIndexPath) as? YourCustomCellClass {
cell.button.isHidden = true
}
}
或者,如果您不想要可选的if let cell
,您可以强制展开,如下所示:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
let cell = collectionView.cellForItem(at: savedIndexPath) as! YourCustomCellClass
cell.button.isHidden = true
}
请注意,savedIndexPath
是要在其中隐藏按钮的单元格的indexPath,而YourCustomCellClass
是您正在使用的UICollectionViewCell
的子类。
发布于 2018-04-05 14:51:42
考虑到你的音频文件,url是唯一的,那么你可以得到player.url
,然后找到这个url的索引,然后你就可以得到indexPath了。
例如
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print(player.url ?? "play")
if let playerFileName = player.url?.lastPathComponent, let index = urls.index(of: playerFileName), let cell = collectionView.cellForItem(at: IndexPath(row: index, section: 0)) as? AudioCollectionViewCell {
cell.playButton.isHidden = true
}
}
如果不是这样,那么将当前的编排索引保存在某个地方,然后您可以在该索引处找到单元格并隐藏/取消隐藏按钮。
https://stackoverflow.com/questions/49672757
复制相似问题