我现在停留了一段时间,虽然我想我知道是什么问题,但仍然无法解决。我正在使用tableView,每个单元格都有倒计时栏动画。每个单元都有用户设置的自定义持续时间。然后,随着时间的流逝,条形动画就会滑动。
我正在使用CADisplayLink来使该动画运行,此代码是写在控制器文件中的,而我只是随着时间的流逝而更改UIView的宽度:

@objc func handleProgressAnimation() {
    let currenttime = Date()
    let elapsed = currenttime.timeIntervalSince(animationStartDate)
    let totalWidth: Double = 400.0
    print(elapsed)
    let arrayLength = durationBar.count
    var i: Int = 0



    for  _ in 0..<arrayLength {
       let indexPath = IndexPath(row: i, section: 0)
       let percentage = (elapsed / Double(durationBar[i].durationTime))
       let newWidth = Double(totalWidth  - (totalWidth * percentage))
        durationBar[i].width = newWidth
        if (percentage < 1)
        {
            if let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? {
                cell.timeRemainingView.frame.size.width = CGFloat(durationBar[indexPath.row].width)
            }
        }
        else {
            if let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? {
                cell.timeRemainingView.frame.size.width = 400
                cell.timeRemainingView.isHidden = true
            }
        }
        i = i + 1
    }

}
当所有条减少时,一切正常,但是随着其中一个单元的时间完成,则UIView宽度变为零,然后随着用户滚动该单元被重用,这将使进度条从还有时间的其他单元中消失。
我正在使用prepareforsegue()方法将条形的宽度重置为400(默认值),但这似乎不起作用。
这是cellforRow代码:
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! listTableViewCell
    cell.testlabel.text = "\(duration[indexPath.row].durationTime)"
    cell.timeRemainingView.frame.size.width = CGFloat(durationBar[indexPath.row].width)

    return cell

}
这是PrepareForReuse()
 override func prepareForReuse() {
    super.prepareForReuse()
    self.timeRemainingView.frame.size.width = 400
    self.timeRemainingView.isHidden = false
}
ios - PrepareForReuse()重置UIView宽度不起作用-LMLPHP
这是屏幕截图。您可以在某些单元格中看到时间到了,但是在其他单元格中仍然有剩余时间,但是进度条消失了。

最佳答案

您必须再次设置框架。 宽度是只读属性。

10-07 19:21
查看更多