【问题标题】:UITableView Cell overlaid with UIProgressView and updates as audio playsUITableView Cell 覆盖有 UIProgressView 并随着音频播放而更新
【发布时间】:2018-08-26 23:09:17
【问题描述】:

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

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

@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")
                }
            }
        }
    }
}

【问题讨论】:

    标签: ios uitableview swift4 uiprogressview


    【解决方案1】:

    您可以在 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
    }}
    

    【讨论】:

    • 感谢您的回复,@pradip。我仅限于以编程方式添加UIProgressView。我已经编辑了我的帖子并添加了到目前为止我想出的代码 sn-p。此功能与UIGestureRecognizer 绑定,其中if sender.state == .began 启动(长按单元格播放相应的音频。音频播放得很好,但progressView 没有启动。如果您可以提供任何进一步的建议,我很感激它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2021-10-29
    相关资源
    最近更新 更多