簡體   English   中英

將CADisplayLink與tableViewCells一起使用所需的指導

[英]Guidance needed on using CADisplayLink with tableViewCells

我正在創建一個包含列表的應用程序,該列表將在一定時間內有效。 為此,我在每個單元格上創建一個倒計時條動畫。 每個單元格都有其自己的自定義持續時間,此后單元格將顯示“時間到”。 該單元甚至在滴答到0時顯示時間(以秒為單位),然后顯示“ Time's Up”消息。

在此上方,有一個倒計時欄動畫。 我正在控制來自viewController的所有這些動畫,而不是在customView單元中執行這些動畫,因為單元的可重用性使計時器陷入困境。

我正在使用兩個計時器:

  1. Timer() :每1秒調用一次。 這用於簡單的秒倒計時。 我正在使用struct類型的數組,該數組包含兩個變量durationTimewidth 默認情況下, width設置為400。 durationTime由用戶設置。

     @objc func handleCountdown() { let arrayLength = duration.count var i: Int = 0 var timeCount: Int = 0 for _ in 0..<arrayLength { let value = duration[i].durationTime - 1 //Decreasing the timer every second let indexPath = IndexPath(row: i, section: 0) if (value > 0) { duration[i].durationTime = value if let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? { cell.testlabel.text = "\\(value)" } //listTableView.reloadRows(at: [indexPath], with: .automatic) i = i + 1 } else { if let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? { cell.testlabel.text = "Time's Up" } i = i + 1 timeCount = timeCount + 1 } } if (timeCount == (arrayLength - 1)) { self.timer?.invalidate() } } 

上面的代碼每秒被調用一次。 它遞減時間的值,然后將其顯示在表格視圖單元格中。 上面的代碼可以正常工作。

  1. CADisplayLink :此計時器用於運行進度條動畫。 但是,在這種情況下,我要計算經過的時間,然后將其除以每個元素的durationTime並計算百分比。 該百分比用於更新每個進度條的width值。 (請記住,durationBar只是duration數組的重復項。但是,在此數組中,durationTime不會遞減->僅用於測試目的)這是代碼:

     @objc func handleProgressAnimation() { let currenttime = Date() let elapsed = currenttime.timeIntervalSince(animationStartDate) let totalWidth: Double = 400.0 print(elapsed) let arrayLength = duration.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 let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? { cell.timeRemainingView.frame.size.width = CGFloat(durationBar[indexPath.row].width) } i = i + 1 } } 

問題 :經過一段時間,即使時間尚未完成,一些進度條也會從單元格中消失。 一旦在tableview上進行了一些滾動,它就會經常發生。

這是willDisplayCellcellForRowIndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! listTableViewCell
    return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
   let cell:listTableViewCell = cell as! listTableViewCell
    cell.testlabel.text = "\(duration[indexPath.row].durationTime)"
    cell.timeRemainingView.frame.size.width = CGFloat(duration[indexPath.row].width)

}

正如您在下面的圖片中可以看到的那樣,一段時間后,即使還有一些時間,進度條也消失了:

在此處輸入圖片說明

這里的問題是,我正在從viewController而不是單元中控制所有動畫和計時器,以防止單元可重用性成為問題。 需要幫助!

問題是您使用以下命令更新cell.timeRemainingView.frame.size.width

durationBar[indexPath.row].width handleProgressAnimation

duration[indexPath.row].width willDisplayCell

而且我也將切換到tableView.indexPathsForVisibleRows以僅針對可見單元格更新UI,因此它不會為if-let中的每個單元格調用.cellForRow。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM