【发布时间】:2020-10-28 08:03:53
【问题描述】:
我有一个集合视图单元格,它有两个视图。一个是按钮,另一个是水平滑块。我想要做的是我想在点击单元格按钮时播放曲目,一旦曲目开始播放,滑块的值应该增加。
当点击播放按钮时,我需要访问 UI 滑块并更新它的值。这是我尝试过的;
按钮点击:
@IBAction func playPreviewButtonTapped(sender: UIButton) {
playPreviewSound(fileName: fileName)
setPausePreviewSoundButtonImage(button: sender)
}
播放声音功能:
private func playPreviewSound(fileName: String) {
let url = Bundle.main.url(forResource: fileName, withExtension: "mp3")
do {
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url!, fileTypeHint: AVFileType.mp3.rawValue)
guard let player = player else { return }
player.play()
setIsPreviewPlaying(state: true)
let selectedIndexPath = cellIndexPathDict[fileName]!.indexPath
let asset = AVAsset(url: url!)
let audioDuration = asset.duration
let audioDurationSeconds = Float(CMTimeGetSeconds(audioDuration))
getCellByIndexPath(indexPath: selectedIndexPath).previewSoundSlider.maximumValue = audioDurationSeconds
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateSliderWithTimer), userInfo: ["slider": getCellByIndexPath(indexPath: selectedIndexPath).previewSoundSlider], repeats: true)
} catch let error {
print(error.localizedDescription)
}
}
private func getCellByIndexPath (indexPath: IndexPath) -> BackStageCollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BackStageCollectionViewCell.REUSE, for: indexPath) as! BackStageCollectionViewCell
setCellInstances(cell: cell, indexPath: indexPath)
print("cell title; ", cell.processorNameLabel.text!) // here, the title always the default title.
return cell
}
定时器功能:
@objc private func updateSliderWithTimer(slider: UISlider) {
let userInfo = timer.userInfo as! Dictionary<String, AnyObject>
let slider: UISlider = (userInfo["slider"] as! UISlider)
print(slider.maximumValue) // here, the max value is always 1
slider.value += 0.5
}
我如何访问单元格?我创建了一本字典;
private var cellIndexPathDict: [String: CellIndexModel] = [:]
struct CellIndexModel {
var indexPath : IndexPath
}
推送到关于单元格创建的 dic:
UICollectionViewDelegate 扩展:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BackStageCollectionViewCell.REUSE, for: indexPath) as! BackStageCollectionViewCell
setCellInstances(cell: cell, indexPath: indexPath)
let gearsGroup = listGearsGroups.object(at: indexPath.section) as! GearsGroup
let gear = gearsGroup.listGears.object(at: indexPath.row) as! BaseGear
let mod : CellIndexModel = CellIndexModel(indexPath: indexPath)
cellIndexPathDict[gear.previewSoundFile as String] = mod
cell.playPreviewButton.addTarget(self, action:#selector(playPreviewButtonTapped(sender:)), for: .touchUpInside)
cell.playPreviewButton.accessibilityLabel = gear.previewSoundFile as String?
return cell
}
基本上,我正在创建一个字典来存储单元格索引路径并尝试在 tap 函数中访问它,但我只获得默认值。不是选中的。
点击按钮后如何访问和更新单元格? 我刚开始学习 Swift,所以一个解释会很棒!谢谢
【问题讨论】:
标签: ios swift uicollectionviewcell