【发布时间】:2021-03-21 23:43:16
【问题描述】:
我想确保我的单元格具有与剩余时间相关的背景。从某种意义上说,我越接近 0,我就越希望它减少,以便我们了解计时器即将到期。
根据经过的时间,它会自动从右到左减少。 这是我在管理 Cell 时使用的代码
class TimerCell: UITableViewCell {
@IBInspectable var defaultBackgroundColor: UIColor = .white
@IBInspectable var runningBackgroundColor: UIColor = .white
@IBInspectable var pausedBackgroundColor: UIColor = .white
@IBInspectable var animationDuration: Double = 0
@IBOutlet var timeLabel: UILabel!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var startButton: UIButton!
@IBOutlet var pauseButton: UIButton!
@IBOutlet var stopButton: UIButton!
weak var timer: Timer? {
didSet {
guard let timer = timer else {
updater?.invalidate()
return
}
if case .running = timer.state {
startUpdater()
}
configure(animated: false)
}
}
private weak var updater: Foundation.Timer?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setEditing(_ editing: Bool, animated: Bool) {
print("*** \(Date()) setEditing(\(editing), animated: \(animated)) (timer?.name: \(String(describing: timer?.name)))")
super.setEditing(editing, animated: animated)
configure(animated: animated)
}
func configure(animated: Bool = true) {
guard let timer = timer else {
return
}
UIView.animate(withDuration: animated ? animationDuration : 0) {
guard !self.isEditing else {
self.timeLabel.text = timer.initialTime.hmsString
self.startButton.safelySetIsHidden(true)
self.pauseButton.safelySetIsHidden(true)
self.stopButton.safelySetIsHidden(true)
self.backgroundColor = self.defaultBackgroundColor
return
}
self.timeLabel.text = ceil(timer.timeForState).hmsString
self.nameLabel.text = timer.name
switch timer.state {
case .stopped:
self.stopButton.safelySetIsHidden(true)
self.pauseButton.safelySetIsHidden(true)
self.startButton.safelySetIsHidden(false)
self.backgroundColor = self.defaultBackgroundColor
case .running:
self.startButton.safelySetIsHidden(true)
self.stopButton.safelySetIsHidden( ceil(timer.timeForState) == 0 ? true : false )
self.pauseButton.safelySetIsHidden( ceil(timer.timeForState) == 0 ? true : false )
self.backgroundColor = self.runningBackgroundColor
case .paused:
self.pauseButton.safelySetIsHidden(true)
self.startButton.safelySetIsHidden(false)
self.stopButton.safelySetIsHidden(false)
self.backgroundColor = self.pausedBackgroundColor
}
}
}
@IBAction private func startTimer() {
timer?.state = .running
configure()
startUpdater()
}
@IBAction private func pauseTimer() {
timer?.state = .paused
configure()
}
@IBAction private func stopTimer() {
timer?.state = .stopped
configure()
}
private func startUpdater() {
guard let timer = timer else {
return
}
let date = Date(timeIntervalSinceNow: timer.timeForState.truncatingRemainder(dividingBy: 1))
let updater = Foundation.Timer(fire: date, interval: 1, repeats: true) {
[weak timer] updater in
self.configure()
if timer?.state != .running {
updater.invalidate()
}
}
self.updater = updater
RunLoop.main.add(updater, forMode: .common)
}
}
【问题讨论】:
-
有什么问题?
-
@matt 我想将根据时间缝合的颜色作为单元格的背景
-
我不知道“根据时间缝合的颜色”是什么意思。如果要更改单元格背景颜色,请更改它。执行此操作的代码在哪里?我没看到。
-
@matt 抱歉,归根结底我想写..
-
问题依然存在。我没有看到你的代码做任何会改变背景颜色的事情。
标签: swift iphone uitableview colors cell