首页 > 解决方案 > UITableView Cell 覆盖有 UIProgressView 并随着音频播放而更新

问题描述

我正在寻找可以以编程方式帮助我添加UIProgressViewover的帖子/链接/指南UITableViewCell。目前,我按住的每个单元格都播放从Firebase流式传输的音频,当我松开时,音频停止。我想添加一个 ProgressView,它首先以清晰的色调覆盖单元格的整个框架,但变成浅蓝色或其他颜色,描绘它从左到右的进度。音频的持续时间将决定进度视图的速度。我搜索过SO,只能找到几年前用Obj C写的类似帖子;寻找用最新的 Swift 语言编写的东西。下面是我在 Google 上搜索的一个随机 gif,它显示了我正在寻找的内容。任何帮助是极大的赞赏。

在此处输入图像描述

更新 1: 我设法获得持续时间并申请进度。UIProgressView相应地记录进度从左到右的持续时间。现在唯一的问题是,当我按下任何单元格以播放其各自的音频时,第一个单元格的进度条会播放。下面是我正在使用的代码。

@objc func updateProgress(){
    let duration = Float((player.currentItem?.duration.seconds)!)
    let currentTime = Float((player.currentItem?.currentTime().seconds)!)
    let progressTotal = currentTime/duration

    let indexPath = IndexPath(row: self.tag, section: 0)
    if progressTotal > 0 {
        if let cell = tableView.cellForRow(at: indexPath) as? PostTableViewCell {
            if currentTime != duration || currentTime/duration != 1 {
                cell.progressBar.progress = progressTotal
                cell.progressBar.progressTintColor = UIColor.blue
            } else {
                timer?.invalidate()
                if timer != nil {
                    timer?.invalidate()
                    timer = nil
                    print("Timer Stopped")
                }
            }
        }
    }
}

标签: iosuitableviewswift4uiprogressview

解决方案


您可以在 tableview 单元格中使用UIProgressView和由如下扩展设置的进度条的动态高度

extension UIProgressView {

@IBInspectable var barHeight : CGFloat {
    get {
        return transform.d * 2.0
    }
    set {
        // 2.0 Refers to the default height of 2
        let heightScale = newValue / 2.0
        let c = center
        transform = CGAffineTransform(scaleX: 1.0, y: heightScale)
        center = c
    }
}}

你可以从情节提要中设置它 在此处输入图像描述

现在您可以通过重新加载单个单元格来管理进度,请参考下面我使用时间完成的代码,您可以使用歌曲计时器来管理,

class ProgressCell: UITableViewCell {
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var progressBar: UIProgressView!}

class ViewController: UIViewController {

var progress = 0.0
var progressTimer: Timer!

@IBOutlet weak var tblProgress: UITableView!

func reloadProgress(at index: Int) {
    let indexPath = IndexPath(row: index, section: 0)

    // start the timer
    progressTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: ["indexPath":indexPath], repeats: true)
}

// called every time interval from the timer
@objc func timerAction() {
    progress += 0.05
    if Int(progress) == 1 {
        progressTimer.invalidate()
    }else{
    let indexPath = (progressTimer.userInfo as! [String:Any])["indexPath"] as! IndexPath
    tblProgress.reloadRows(at: [indexPath], with: .none)
    }
}}

extension ViewController: UITableViewDataSource,UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tblProgress.dequeueReusableCell(withIdentifier: "ProgressCell") as! ProgressCell

    cell.lblTitle.text = "house of highlights"
    cell.progressBar.progress = Float(progress)
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    progress = 0.0
    reloadProgress(at: indexPath.row)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}}

推荐阅读